GridView DoubleClick

时间:2010-12-05 20:34:04

标签: wpf xaml listview wpf-controls listviewitem

我有一个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>

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

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

3 个答案:

答案 0 :(得分:12)

您可以将MouseDoubleClick事件添加到ItemContainerStyle中的ListViewItem,就像这样

<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)
{
    //...            
}

答案 1 :(得分:10)

如果您正在使用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,以及ICommand属性{{1}需要一个字符串参数):

SayHiCommand

答案 2 :(得分:1)

对于此处使用Prism之类的框架的用户,其中包含Listview的主要用户控件是绑定到视图模型的View(不是窗口)。

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

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

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

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