我尝试使用AttachedCommandBehavior V2将ListBoxItem事件(如双击)转换为针对视图模型执行的命令。使用样式应用多个AttachedCommandBehaviors
我想火的多个事件的命令,这是我试图效仿的例子代码:
<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>
因为我要适用于一个ListBoxItem的,我想通过样式做这样做:
<ListBox.ItemContainerStyle>
<Style>
<Setter Property="acb:CommandBehaviorCollection.Behaviors">
<Setter.Value>
<acb:CommandBehaviorCollection>
<acb:BehaviorBinding Event="MouseDoubleClick" Command="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}"/>
<acb:BehaviorBinding Event="KeyUp" Command="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}"/>
</acb:CommandBehaviorCollection>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
但我得到的代码,说error MC3089: The object 'CommandBehaviorCollection' already has a child and cannot add 'BehaviorBinding'. 'CommandBehaviorCollection' can accept only one child. Line 39 Position 11.
而且一个编译错误,如果我注释掉BehaviorBindings之一,那么它COMP iles,但我得到一个运行时xaml加载异常说“值不能为空。参数名:财产”,所以我不知道如果我即使考虑正确的做法
谁能提供正确的语法的一个例子,在ListBoxItem中设置多个行为的绑定
我发现[相关的问题](http://stackoverflow.com/questions/1035023/firing-a-double-click-event-from-a-wpf-listview-item-using-mvvm),显示使用样式仅设置单个CommandBehavior的示例 – BrandonAGr 2012-03-15 23:04:23
您现在尝试使用您找到的文章中所述的两个交互触发器吗?这就是我要建议的。 – Phil 2012-03-16 10:26:30