简体   繁体   中英

WPF: List-View: Binding double-click event on ListView Items using MVVM pattern

Using MVVM pattern I am binding the Item source of a ListView control, binded double click event using the below xaml code,

Implemented using:

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

when i double click on the listview items am not able to perform my functionality.

How can i attach a double click event in MVVM pattern effecitively??

i use this in my projects.

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

ok for ListView you have to set the Binding to the 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>

or you use the interactionstuff

<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>

If you want to do it in a code-behind, you can do like this

EDIT

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>

Code-Behind

   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));
            }
        }
    }
}

Item Class :

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM