如何在列表视图的 mvvm 中绑定 MouseDoubleClick 事件

本文关键字:绑定 MouseDoubleClick 事件 mvvm 列表 视图 | 更新日期: 2023-09-27 18:30:17

我有一个包含多列的列表视图。

我想在双击一行时运行命令。我为此使用EventTrigger。但是当我双击列表视图运行命令的每个位置时。但是我想在双击时运行此命令 ListViewItem .

<i:Interaction.Triggers>
  <i:EventTrigger EventName="MouseDoubleClick">
    <cmd:EventToCommand Command="{Binding ShowLetterCommand}" CommandParameter="{Binding SelectedItem, ElementName=DashboardListView}" />
  </i:EventTrigger>
</i:Interaction.Triggers>

如何在列表视图的 mvvm 中绑定 MouseDoubleClick 事件

您应该从 System.Windows.Interactions 库中调用 CallMethodAction,并将您的方法名称指向MethdoName如下所示:

我做了一个例子,它完全有效:)。您需要两个库来处理MouseDoubleClick事件:

  • System.Windows.Interactivity(我的电脑地址是C:'Program Files (x86)'Microsoft SDKs'Expression'Blend'.NETFramework'v4.5'Libraries'
  • Microsoft.表达式.交互(我的电脑上的地址是C:'Program Files (x86)'Microsoft SDKs'Expression'Blend'.NETFramework'v4.5'Libraries'

XAML:

<UserControl
 ...the code omitted for the brevity...
  xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
  xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
...the code omitted for the brevity...
>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="5*"/>
    </Grid.ColumnDefinitions>             
   <ListView ItemsSource="{Binding Persons}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <ei:CallMethodAction MethodName="DoubleClickMethod" TargetObject="{Binding}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding IdPerson}" Margin="0,0,5,0"/>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>                
        </ListView.ItemTemplate>
    </ListView>
</Grid>
</UserControl>

视图模型:

public void DoubleClickMethod()
{
   MessageBox.Show("It is a Double Click");
   /*        if(parameter!=null)
       YourClass aClass=(YourClass)parameter; 
   */
}
private ObservableCollection<Person> persons;
public ObservableCollection<Person> Persons
{
   get { return persons; }
   set
   {
      persons = value;
      OnPropertyChanged("Persons");
   }
}
public MainWindowViewModel()
{
   LoadPersons();
}
private void LoadPersons()
{
   persons = new ObservableCollection<Person>();
   for (int i = 0; i < 20; i++)
   {
      Persons.Add(new Person() { IdPerson = i, Name = "Charlie " + i.ToString()});
   }

}

型:

public class Person
{
    public int IdPerson { get; set; }
    public string Name { get; set; }
}

用于下载测试项目的链接。

您可以使用

Inputbindings来实现此目的。

<ListView ItemsSource="{Binding SampleListData,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedTree,Mode=TwoWay}" Name="dgSample">
        <ListView.InputBindings>
            <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ListDataEdited}"/>
        </ListView.InputBindings>
</ListView>

将所选行绑定到"选定项"。

查看型号 :

 #region Commands
    public RelayCommand ListDataEdited
    {
        get
        {
            return new RelayCommand(listDataEdited);
        }
    }
    #endregion
    #region Methods
    private void listDataEdited()
    {
        if (SelectedTree != null)
        {
            //code here
        }
    }
    #endregion