【问题标题】:GridView DoubleClickGridView 双击
【发布时间】:2010-12-05 20:34:04
【问题描述】:

我有一个 GridView,我想在列表中的项目上检测双击事件,我这样做如下:

<ListView>
    <ListView.View >
        <GridView >
            <GridViewColumn Header="FileName">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding FileName}" MouseDoubleClick="Configuration_MouseDoubleClick"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding CreationDate}" Header="Date"/>
        </GridView>
     </ListView.View>
</ListView>

问题是我只能通过将双击附加到模板中的控件来检测双击。

如何将MouseDoubleClick 事件附加到整个ListViewItem? PRISM 有什么解决方案吗?

【问题讨论】:

    标签: wpf xaml listview wpf-controls listviewitem


    【解决方案1】:

    你可以像这样在ItemContainerStyle中的ListViewItem中添加MouseDoubleClick事件

    <ListView ...>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
    

    后面的代码..

    void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        //...            
    }
    

    【讨论】:

      【解决方案2】:

      如果您正在使用 MVVM,您可以按照通常的方式弥合代码隐藏和视图模型之间的差距——通过使用附加行为:

      using System;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Input;
      
      public sealed class HandleDoubleClickBehavior
      {
          public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
              "Command", typeof (ICommand), typeof (HandleDoubleClickBehavior), new PropertyMetadata(default(ICommand), OnComandChanged));
      
          public static void SetCommand(DependencyObject element, ICommand value)
          {
              element.SetValue(CommandProperty, value);
          }
      
          public static ICommand GetCommand(DependencyObject element)
          {
              return (ICommand) element.GetValue(CommandProperty);
          }
      
          public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
              "CommandParameter", typeof (object), typeof (HandleDoubleClickBehavior), new PropertyMetadata(default(object)));
      
          public static void SetCommandParameter(DependencyObject element, object value)
          {
              element.SetValue(CommandParameterProperty, value);
          }
      
          public static object GetCommandParameter(DependencyObject element)
          {
              return (object) element.GetValue(CommandParameterProperty);
          }
      
          private static void OnComandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              var c = d as Control;
              if (c == null)
                  throw new InvalidOperationException($"can only be attached to {nameof(Control)}");
              c.MouseDoubleClick -= OnDoubleClick;
              if (GetCommand(c) != null)
                  c.MouseDoubleClick += OnDoubleClick;
          }
      
          private static void OnDoubleClick(object sender, MouseButtonEventArgs e)
          {
              var d = sender as DependencyObject;
              if (d == null)
                  return;
              var command = GetCommand(d);
              if (command == null)
                  return;
              var parameter = GetCommandParameter(d);
              if (!command.CanExecute(parameter))
                  return;
              command.Execute(parameter);
          }
      }
      

      在您的工具箱中,您可以像这样编写 XAML(假设 PersonViewModel 包含字符串属性 NameTitle,以及一个名为 SayHiCommandICommand 属性,它需要一个字符串参数):

      <ListView ItemsSource="{Binding Persons}" >
          <ListView.ItemContainerStyle>
              <Style TargetType="ListViewItem">
                  <Setter Property="local:HandleDoubleClickBehavior.Command" Value="{Binding SayHiCommand}" />
                  <Setter Property="local:HandleDoubleClickBehavior.CommandParameter" Value="{Binding Name}" />
              </Style>
          </ListView.ItemContainerStyle>
          <ListView.View>
              <GridView>
                  <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                  <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" />
              </GridView>
          </ListView.View>
      </ListView>
      

      【讨论】:

        【解决方案3】:

        对于那些使用像 Prism 这样的框架的用户,其中包含 Listview 的主用户控件是绑定到视图模型的视图(不是窗口)。

        dlf 的答案是最好的这些小调整:

        {Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, 
                 Path=DataContext.SayHiCommand}
        

        在特殊行为附加属性中,您将 ICommand 强制转换为 DelegateCommand 强制转换。

        像魅力一样工作, 非常感谢。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-09
          • 2018-02-01
          • 1970-01-01
          • 2017-07-26
          • 1970-01-01
          相关资源
          最近更新 更多