WPF : InputBindings on a StackPanel

标签 wpf xaml binding command stackpanel

我想在 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/

相关文章:

wpf - 是否有任何好的文档或网站显示 XAML GUI 的设计指南?

c# - 如何在没有科学表示的情况下格式化 XAML 中的 double 值以在末尾删除零?

wpf - 如何限制 <Border> 元素的宽度和高度等于其内部内容?

c# - 通用 Windows 应用程序项目中的 MSBuild 错误

java - 动态绑定(bind)Java

c# - 我的 WPF 自定义控件 Datacontext 正在取代父控件

c# - 根据 WPF 中的情况自定义菜单

wpf - x :Reference? 的替代品是什么

c# - 在 XAML 中处理 Button 的 Click 事件?

c# - 从另一个ViewModel访问属性