【问题标题】:Adding MouseBindings to Items in a databound WPF ListView将 MouseBindings 添加到数据绑定 WPF ListView 中的项目
【发布时间】:2011-01-09 03:35:46
【问题描述】:

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

<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 样式并替换ListViewItemControlTempalte 获得了解决方案。不过,我希望有一个更简单的解决方案。

真诚地, 迈克尔

【问题讨论】:

标签: wpf data-binding listview mvvm icommand


【解决方案1】:

使用样式替换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'。”当我尝试从一种风格中使用它时。当我不把它放在样式中时它会起作用。
  • 我想你可能没有使用 propa sn-p。如果你有,它会为你放入“静态”。此外,propa 应该通过正确设置 DependencyProperty 来解决您的其他问题。您是否将第三个参数设置为RegisterAttached
  • 在 WPF 3.5 中,MouseBinding 的 Command 属性不是 DependencyProperty,所​​以这段代码被剪掉了。这在我使用 atm 的 WPF 4.0 中已修复。见:blogs.msdn.com/llobo/archive/2009/10/29/…
  • AddToInputBinding 的一个问题是Styles 在第一次应用时就被冻结,因此每个ListViewItem 都会绑定到第一个项目的视图模型中的命令。 .
【解决方案2】:

我能够通过执行以下操作来解决此问题:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多