2010-12-05 31 views
2

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

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

的问题是,我只能通过它连接到检测doubleclicks模板中的控件。

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

回答

12

可以MouseDoubleClick事件在ItemContainerStyle像这背后

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

代码添加到ListViewItem的..

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

欢呼Meleak,这很好地工作! – thumbmunkeys 2010-12-05 20:49:19

+0

@pivotnig:很高兴听到:) – 2010-12-05 20:55:06

8

如果你做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>