[英]WPF: List-View: Binding double-click event on ListView Items using MVVM pattern
使用 MVVM 模式我正在绑定 ListView 控件的 Item 源,使用以下 xaml 代码绑定双击事件,
使用:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<z:EventToCommand Command="{Binding RelativeSource={RelativeSource TemplatedParent},Path=MouseDoubleClick}"/>
</i:EventTrigger>
当我双击列表视图项时,我无法执行我的功能。
如何有效地在 MVVM 模式中附加双击事件?
我在我的项目中使用它。
<DataGrid.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding Path=EditEntityCommand}"
CommandParameter="{Binding ElementName=DataGrid, Path=SelectedItem}"/>
</DataGrid.InputBindings>
好的,对于 ListView,您必须将 Binding 设置为 ListViewItems
<ListView x:Name="listView1" Grid.Row="2" ItemsSource="{Binding VmUsers}">
<ListView.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding}">
<ContentPresenter.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding DataContext.MyCommand, ElementName=listView1}"
CommandParameter="{Binding ElementName=listView1,Path=SelectedItem}"/>
</ContentPresenter.InputBindings>
</ContentPresenter>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
或者你使用交互工具
<ListView Name="listView1" ItemsSource="{Binding Cars}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LeftDoubleClick">
<i:InvokeCommandAction Command="{Binding ItemSelectCommand}" CommandParameter="{Binding ElementName=listView1,Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
如果你想在代码隐藏中做到这一点,你可以这样做
编辑
XAML
<Window x:Class="Q9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Q9"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView ItemsSource="{Binding Cars}" SelectedItem="{Binding SelectedCar,Mode=TwoWay}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding Path=DataContext.ShowCarInformationCommand, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"/>
</Grid.InputBindings>
<TextBlock Text="{Binding Name}" Height="30" HorizontalAlignment="Stretch"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
代码隐藏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Q9
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
}
视图模型
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Q9
{
public class ViewModel : System.ComponentModel.INotifyPropertyChanged
{
private ShowCarInformationCommand mShowCarInformationCommand;
public ShowCarInformationCommand ShowCarInformationCommand
{
get
{
if (mShowCarInformationCommand == null)
{
mShowCarInformationCommand = new ShowCarInformationCommand(this);
}
return mShowCarInformationCommand;
}
set
{
mShowCarInformationCommand = value;
}
}
private System.Collections.ObjectModel.ObservableCollection<Car> mCars;
public System.Collections.ObjectModel.ObservableCollection<Car> Cars
{
get
{
if (mCars == null)
{
mCars = new System.Collections.ObjectModel.ObservableCollection<Car>();
}
return mCars;
}
set
{
mCars = value;
}
}
private Car mSelectedCar;
public Car SelectedCar
{
get
{
return mSelectedCar;
}
set
{
mSelectedCar = value;
OnPropertyChanged("SelectedCar");
}
}
public ViewModel()
{
Cars.Add(new Car() { Name = "Honda" });
Cars.Add(new Car() { Name = "Ferrari" });
Cars.Add(new Car() { Name = "Bentley" });
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
项目类别:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Q9
{
public class Car
{
private System.String mName;
public System.String Name
{
get { return mName; }
set { mName = value; }
}
}
public class ShowCarInformationCommand : System.Windows.Input.ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
ViewModel Model;
public ShowCarInformationCommand(ViewModel model)
{
Model = model;
}
public void Execute(object parameter)
{
System.Windows.MessageBox.Show(Model.SelectedCar.Name);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.