WPF: 列表视图: 使用MVVM模式将双击事件绑定到列表视图项目

3

我将使用MVVM模式来绑定ListView控件的Item Source,并使用以下XAML代码绑定双击事件:

实现方式:

  <i:Interaction.Triggers>
     <i:EventTrigger EventName="MouseDoubleClick">
          <z:EventToCommand Command="{Binding RelativeSource={RelativeSource TemplatedParent},Path=MouseDoubleClick}"/>
      </i:EventTrigger>

当我双击列表视图项目时,无法执行我的功能。

如何在MVVM模式下有效地附加双击事件?

2个回答

3

我在我的项目中使用这个。

 <DataGrid.InputBindings>
     <MouseBinding MouseAction="LeftDoubleClick"
             Command="{Binding Path=EditEntityCommand}"
             CommandParameter="{Binding ElementName=DataGrid, Path=SelectedItem}"/>
  </DataGrid.InputBindings>

对于ListView,您需要将绑定设置为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>

嗨,blindmeis,我按照你提到的相同方法进行了操作。<ListView.InputBindings> <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding Path=MouseDoubleClick}"/> </ListView.InputBindings>ViewModel中的RelayCommand实现 this.MouseDoubleClick=new RelayCommand(this.MousedoubleClick);当在listview项上执行双击操作时,此方法未被调用 private void MousedoubleClick() { this.ErrorMessage= string.Format(" Double Click event fired") } 我在实现中有遗漏吗? - Pavan Kumar
很抱歉,i:EventTrigger无法检测到LeftDoubleClick事件,但您可以尝试使用MouseDoubleClick事件。 - luis_laurent

-1
如果你想在代码后台实现它,你可以这样做:
编辑:
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);
        }
    }
}

嗨,Vivek,你有没有想过如何使用MVVM模式实现相同的功能? - Pavan Kumar
你正在使用.NET 4.5吗? - Vivek Saurav
你需要在这里双击文本块..我有空就为你写下来了。 - Vivek Saurav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接