在数据绑定的WPF ListView中为项添加鼠标绑定

3

我正在尝试在用户单击ListView中的项目时执行ViewModel中的命令。当我在XAML中添加一个ListViewItem时,我可以将MouseBinding添加到其InputBindings中。

<ListView>
<ListView.View>
   <GridView>
      <GridViewColumn Header="Test" />
   </GridView>
   </ListView.View>
   <ListViewItem Content="Item 1" >
      <ListViewItem.InputBindings>
         <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}" />
      </ListViewItem.InputBindings>
 </ListViewItem>
 </ListView>

但是在数据绑定的ListView中如何实现这一点呢?
<ListView ItemsSource="{Binding Patients}">
<ListView.View>
    <GridView>
        <GridViewColumn Header="Test" />
    </GridView>
    <!-- How to set the MouseBinding for the generated ListViewItems?? -->
</ListView.View>

我已经通过定义ListViewItem样式并替换ListViewItemControlTemplate来解决了这个问题。虽然,我希望有更简单的解决方案。

真诚地, Michael


你看过这篇文章了吗?https://dev59.com/SHNA5IYBdhLWcg3wQ7Yw - Brent
请查看 social.msdn 上的这篇帖子: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/fb85577c-3704-492e-900c-1f0bffd1e4c2/ - Aviad P.
2个回答

5

使用样式替换ListViewItem上的ControlTemplate并不是一个坏的解决方案。实际上,这可能是我的首选。

另一种实现相同效果的方法是在ListViewItem样式上使用自定义附加属性:

<Style TargetType="ListViewItem">
  <Setter Property="local:AddToInputBinding.Binding">
    <Setter.Value>
      <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}" />    
    </Setter.Value>
  </Setter>
  ...

为了实现这一点,您需要创建MyBindingHandler.AddBinding附加属性:
public class AddToInputBinding
{
  public static InputBinding GetBinding(... // create using propa snippet
  public static void SetBinding(...
  public static readonly DependencyProperty BindingProperty = DependencyProperty.RegisterAttached(
    "Binding", typeof(InputBinding), typeof(AddToInputBinding), new PropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
    {
      ((UIElement)obj).InputBindings.Add((InputBinding)e.NewValue);
    }
  }));
}

这个可以扩展成处理多个绑定,但是你已经有了想法:这个类可以让你在任何样式中添加InputBinding。

这种解决方案可能比你当前使用的更好,因为DoubleClick绑定是直接定义在ListBoxItem上,而不是在其模板内的另一个控件上。但我认为这主要取决于个人喜好。


我尝试了你的解决方案,但无法使其工作。首先,我认为GetBinding()和SetBinding()应该是静态的(如果它们不是静态的,则无法编译),其次,当我尝试从样式中使用它时,总是会出现错误“无法将属性'Property'中的值转换为类型'System.Windows.DependencyProperty'的对象。” 如果我不将其放在样式中,则可以正常工作。 - Teodor
我认为你可能没有使用正确的代码片段。如果你使用了,它会自动添加“static”关键字。此外,正确使用propa应该可以通过正确设置DependencyProperty来解决你的其他问题。你有将第三个参数设置为RegisterAttached吗? - Ray Burns
在WPF 3.5中,MouseBinding的Command属性不是DependencyProperty,因此这段代码将无法工作。这在我目前使用的WPF 4.0中已经修复。参见:http://blogs.msdn.com/llobo/archive/2009/10/29/new-wpf-features-key-gesture-binding.aspx - Michael Menne
AddToInputBinding 的一个问题是,一旦应用了 Style,它们就会被冻结,因此每个 ListViewItem 都会绑定到第一个项目的视图模型的命令... - dlf

1
我通过以下方式解决了这个问题:
1)我添加了对System.Windows.Interactivity DLL的引用(在C:\ Program Files(x86)\ Microsoft SDKs \ Expression \ Blend \ .NETFramework \ v4.0 \ Libraries中找到)
2)将此内容添加到我的XAML文件中:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

3) 在我的 ListView 中添加了这个:

<ListView ...>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{x:Static local:MainWindow.RoutedCommandEditSelectedRecordWindow}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    ...

</ListView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接