首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >视图中的MouseDoubleClick事件

视图中的MouseDoubleClick事件
EN

Stack Overflow用户
提问于 2010-05-14 12:54:17
回答 2查看 1.3K关注 0票数 1

当我使用mvvm和Prism 2时,如何在视图中绑定wpfdatagrid的MouseDoubleClick事件。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-14 18:43:13

在视图的代码隐藏中侦听MouseDoubleClick事件,并在ViewModel上调用相应的方法:

代码语言:javascript
复制
public class MyView : UserControl 
{
    ...

    private MyViewModel ViewModel { get { return DataContext as MyViewModel; } }

    private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ViewModel.OpenSelectedItem();
    }
票数 -1
EN

Stack Overflow用户

发布于 2010-09-07 14:15:22

我更喜欢添加一个MouseDoubleClickBehaviour,然后你可以将它附加到任何绑定到你的ViewModel的控件上。从视图的代码隐藏中调用命令创建了我不喜欢的直接依赖项。

代码语言:javascript
复制
public static class MouseDoubleClickBehaviour
{
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(MouseDoubleClickBehaviour), new UIPropertyMetadata(null, OnCommandChanged));

    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(MouseDoubleClickBehaviour), new UIPropertyMetadata(null));

    public static ICommand GetCommand(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(CommandProperty);
    }

    public static void SetCommand(DependencyObject obj, ICommand value)
    {
        obj.SetValue(CommandProperty, value);
    }

    public static object GetCommandParameter(DependencyObject obj)
    {
        return obj.GetValue(CommandParameterProperty);
    }

    public static void SetCommandParameter(DependencyObject obj, object value)
    {
        obj.SetValue(CommandParameterProperty, value);
    }

    private static void OnCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
    {
        var grid = target as Selector;

        ////Selector selector = target as Selector;
        if (grid == null)
        {
            return;
        }

        grid.MouseDoubleClick += (a, b) => GetCommand(grid).Execute(grid.SelectedItem);
    }
}

然后,您可以在XAML中执行此操作

代码语言:javascript
复制
<ListView ...
     behaviours:MouseDoubleClickBehaviour.Command="{Binding Path=ItemSelectedCommand}"
     behaviours:MouseDoubleClickBehaviour.CommandParameter="{Binding ElementName=txtValue, Path=Text}"
 .../>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2831911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档