Edw*_*uay 0 wpf mvvm attachedbehaviors
我已经实现了这里找到的附加命令行为模式,它可以很好地允许例如一个Border有一个在ViewModel中触发的左键或右键单击事件:
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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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());
}
};
}
Run Code Online (Sandbox Code Playgroud)
但是,如何将两个附加行为附加到一个元素,例如,我想要执行以下操作,但它当然会给我一个错误:
<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"
>
Run Code Online (Sandbox Code Playgroud)
您发送的链接包含了这个答案.您可以在ACB v2中使用CommandBehaviorCollection.Behaviors功能.
<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test">
<local:CommandBehaviorCollection.Behaviors>
<local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/>
<local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/>
</local:CommandBehaviorCollection.Behaviors>
<TextBlock Text="MouseDown on this border to execute the command"/>
</Border>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5225 次 |
| 最近记录: |