我怎么能把两个连接的行为,以一个XAML元素?
-
06-09-2019 - |
题
我实现发现附命令行为模式这里它的效果很好,以允许如边框具有左或右点击事件,在视图模型火灾:
<强> 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"
>
解决方案
您发送的链接包含了非常答案。您可以使用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>
其他提示
的附着性能‘行为’型‘CommandBehaviorCollection’没有被发现。‘虽然我可以运行和编译罚款,这是为什么“虽然我的XAML编辑给我的错误就是这样,谢谢,搞笑’? “
的原因是,其允许命令行为集合的代码(这是一个附加属性的集合)是actualy排序一个XAML漏洞。你可以阅读更多有关在这里: http://wekempf.spaces.live.com/博客/ CNS!D18C3EC06EA971CF!468.entry?SA = 276442122
不隶属于 StackOverflow