使用MVVM模式我使用下面的xaml代码绑定ListView控件的Item源,绑定双击事件,
使用:
实施README
当我双击listview项目时,我无法执行我的功能。
如何有效地在MVVM模式中附加双击事件?
答案 0 :(得分:1)
我在我的项目中使用它。
B
对于ListView,您必须将Binding设置为ListViewItems
<DataGrid.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding Path=EditEntityCommand}"
CommandParameter="{Binding ElementName=DataGrid, Path=SelectedItem}"/>
</DataGrid.InputBindings>
或您使用interactionstuff
<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>
答案 1 :(得分:0)
如果你想在代码隐藏中执行此操作,可以这样做
修改强>
<强> 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();
}
}
}
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);
}
}
}