我已經實施了attached command behavior pattern found here並且它很好地運作以允許例如邊境到有火災視圖模型一個左或右鍵單擊事件:如何將兩個附加行爲附加到一個XAML元素?
XAML:
<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2"
c:CommandBehavior.Event="MouseLeftButtonDown"
c:CommandBehavior.Command="{Binding PressedLeftButton}"
c:CommandBehavior.CommandParameter="MainBorder123">
<TextBlock Text="this is the click area"/>
</Border>
代碼背後:
public ICommand PressedLeftButton { get; private set; }
public MainViewModel()
{
Output = "original value";
PressedLeftButton = new SimpleCommand
{
ExecuteDelegate = parameterValue => {
Output = String.Format("left mouse button was pressed at {0} and sent the parameter value \"{1}\"", DateTime.Now.ToString(), parameterValue.ToString());
}
};
}
然而,怎麼辦我將兩個附加行爲附加到一個元素,例如我想要做類似下面的,但它當然給我一個錯誤:
<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2"
c:CommandBehavior.Event="MouseLeftButtonDown"
c:CommandBehavior.Command="{Binding PressedLeftButton}"
c:CommandBehavior.CommandParameter="MainBorder123"
c:CommandBehavior.Event="MouseRightButtonDown"
c:CommandBehavior.Command="{Binding PressedRighttButton}"
c:CommandBehavior.CommandParameter="MainBorder123"
>
就是這樣,謝謝,不過搞笑的是我的XAML編輯給我的錯誤「的附着性能‘行爲’型‘CommandBehaviorCollection’沒有被發現。」雖然我可以運行並編譯它,但爲什麼呢? – 2009-05-29 15:21:48