如何在ListView的mvvm中绑定MouseDoubleClick事件

时间:2016-01-13 09:48:26

标签: c# wpf listview mvvm

我有一个包含多列的ListView。

我想在双击一行时运行一个命令。我为此使用了EventTrigger。但是,当我双击 ListView 运行命令的每个位置时。但是我想在双击ListViewItem时运行此命令。

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

2 个答案:

答案 0 :(得分:3)

您应该从 System.Windows.Interactions 库中调用CallMethodAction,并在MethdoName处指向您的方法名称:

我已经做了一个例子,它完美地工作:)。 您需要两个库来处理MouseDoubleClick事件:

  • System.Windows.Interactivity (我计算机上的地址为C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\
  • Microsoft.Expression.Interactions (我的计算机上的地址为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; }
}

The link to download a test project

答案 1 :(得分:0)

您可以使用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>

将所选行绑定到&#39; SelectedItem&#39;

查看型号:

 #region Commands
    public RelayCommand ListDataEdited
    {
        get
        {
            return new RelayCommand(listDataEdited);
        }
    }
    #endregion

    #region Methods
    private void listDataEdited()
    {
        if (SelectedTree != null)
        {
            //code here
        }
    }
    #endregion