我想在 ListBoxItem 上放置一个命令。 ListBoxItem 使用由 StackPanel 组成的 DataTemplate(包含 Image 和 TextBlock,两者都使用 Binding)。我希望双击该 ListBoxItem 来触发该命令。
我已经尝试过这个:
<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>
我还尝试将命令资源放在包含此 StackPanel 的 StackPanel 上,而不进行任何更改。 我确信我的绑定(bind),因为当我将 InputBindings 部分放在 TextBlock 上时,它就可以工作。
谢谢
最佳答案
尝试在 ListBox
而不是 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>
关于WPF : InputBindings on a StackPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1104317/