我嘗試使用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