1

I am trying to use AttachedCommandBehavior V2 to translate ListBoxItem events such as double click into commands that are execute against the view model.

I want to fire commands for multiple events, this is the example code I am trying to emulate:

<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>

Since I want to apply that to a ListBoxItem, I am trying to do it through a style by doing:

<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>

But I get a compile error with that code that says error MC3089: The object 'CommandBehaviorCollection' already has a child and cannot add 'BehaviorBinding'. 'CommandBehaviorCollection' can accept only one child. Line 39 Position 11.

Also if I comment out one of the BehaviorBindings then it compiles but I get a runtime xaml load exception saying "Value cannot be null. Parameter name: property", so I'm not sure if I'm even taking the correct approach.

Can anyone provide an example of the correct syntax to set multiple behavior bindings on a ListBoxItem?

BrandonAGr
  • 5,827
  • 5
  • 47
  • 72
  • I found [a related question](http://stackoverflow.com/questions/1035023/firing-a-double-click-event-from-a-wpf-listview-item-using-mvvm) that shows an example for setting only a single CommandBehavior using a style – BrandonAGr Mar 15 '12 at 23:04
  • Have you now tried using two interaction triggers as described in the article you found? That's what I was going to suggest. – Phil Mar 16 '12 at 10:26

1 Answers1

1

My solution uses interaction triggers and the ItemTemplate not the ItemContainerStyle. This invokes a mouse double click or key up command in the text box, not the whole list box item.

<UserControl.Resources>
    <DataTemplate DataType="{x:Type ViewModel:DataItem}" x:Key="ItemTemplate">
        <ContentControl>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <i:InvokeCommandAction Command="{Binding DoubleClickCommand}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="KeyUp">
                    <i:InvokeCommandAction Command="{Binding KeyUpCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <TextBox Text="{Binding Name}">
            </TextBox>
        </ContentControl>
    </DataTemplate>
</UserControl.Resources>

<ListBox x:Name="listBox" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource={Binding Items} />

Where DataItem is something like

class DataItem : INotifyPropertyChanged
{
   public string Name{get;set}
   .. etc
}

and the view model set on the DataContext has an IList<DataItems> Items{get; private set} property.

Phil
  • 42,255
  • 9
  • 100
  • 100