views:

1115

answers:

2

I want to put a command on a ListBoxItem. The ListBoxItem use a DataTemplate composed of a StackPanel (containing an Image and a TextBlock, both using Binding). I want that the doubleclick on that ListBoxItem fire the command.

I have tried this :

<DataTemplate>
    <StackPanel>
        <StackPanel.Resources>
            <CommonUI:CommandReference x:Key="DoubleClickCommand" Command="{Binding Path=DefaultCommand}" />                                
        </StackPanel.Resources>
        <StackPanel.InputBindings>
            <MouseBinding Gesture="LeftDoubleClick" Command="{StaticResource DoubleClickCommand}" />
        </StackPanel.InputBindings>
        <Image Source="{Binding Path=Thumbnail, IsAsync=True}" IsHitTestVisible="False"/>
        <TextBlock Text="{Binding Path=Name}" IsHitTestVisible="False">
    </StackPanel>
</DataTemplate>

I have also tried to put the Command Resources on a StackPanel containing this StackPanel, without any change. I am certain of my binding because when I put the InputBindings part on the TextBlock, it works.

Thanks

A: 

Try handling the event in the ListBox instead of the StackPanel:

<ListBox>
  <ListBox.Resources>
    <CommonUI:CommandReference x:Key="DoubleClickCommand" Command="{Binding Path=DefaultCommand}" />
  </ListBox.Resources>
  <ListBox.InputBindings>
    <MouseBinding Gesture="LeftDoubleClick" Command="{StaticResource DoubleClickCommand}" />
  </ListBox.InputBindings>
  <DataTemplate>
    <StackPanel>
      <Image Source="{Binding Path=Thumbnail, IsAsync=True}" />
      <TextBlock Text="{Binding Path=Name}" />
    </StackPanel>
  </DataTemplate>
</ListBox>
Julien Poulin
I have tried it but it doesn't work either :(
Thia
It seems working now. I have tried a couple of things (ending up looking exactly as the code I submitted in my question, excluding the IsHitTestVisible="False"). Thanks anyway :-)
Thia
+2  A: 

My code finally looks like this :

<DataTemplate>
    <StackPanel Orientation="Vertical">
        <StackPanel.Resources>
            <CommonUI:CommandReference x:Key="DoubleClickCommand" Command="{Binding Path=DefaultCommand}" />
        </StackPanel.Resources>
        <StackPanel.InputBindings>
            <MouseBinding Gesture="LeftDoubleClick" Command="{StaticResource DoubleClickCommand}" />
        </StackPanel.InputBindings>
        <Image Source="{Binding Path=Thumbnail, IsAsync=True}" />
        <TextBlock Text="{Binding Path=Name}" />
    </StackPanel>
</DataTemplate>

Thanks anyway, Mr Poulin.

Thia