Search code examples
wpfxamlbindingcommandstackpanel

WPF : InputBindings on a StackPanel


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


Solution

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