【问题标题】:WPF: List-View: Binding double-click event on ListView Items using MVVM patternWPF:List-View:使用 MVVM 模式在 ListView 项上绑定双击事件
【发布时间】:2016-02-17 05:40:02
【问题描述】:

使用 MVVM 模式我正在绑定 ListView 控件的 Item 源,使用下面的 xaml 代码绑定双击事件,

使用实现:

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

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

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

【问题讨论】:

    标签: c# wpf mvvm-light


    【解决方案1】:

    我在我的项目中使用它。

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

    或者你使用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>
    

    【讨论】:

    • 嗨,blindmeis,我采用了与您提到的相同的方法 RelayCommand ViewModel 中的实现 this.MouseDoubleClick=new RelayCommand(this.MousedoubleClick);当对列表视图项执行双击操作时,不会调用此方法 private void MousedoubleClick() { this.ErrorMessage= string.Format(" Double Click event failed") } 我错过了实现中的任何一点吗
    • 恐怕 i:EventTrigger 没有检测到 LeftDoubleClick 但您可以尝试使用 MouseDoubleClick 事件
    【解决方案2】:

    如果你想在代码隐藏中这样做,你可以这样做

    编辑

    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 模式实现相同的功能吗?
    • 您使用的是 .NET 4.5 吗?
    • 你必须在这里双击文本块..我有空闲时间,所以我为你写了下来
    猜你喜欢
    • 2010-11-05
    • 2021-04-21
    • 2017-06-28
    • 2012-06-17
    • 2010-10-18
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多