视图中的 MouseDoubleClick 事件

发布于 2024-09-01 03:35:38 字数 72 浏览 6 评论 0原文

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

How do I bind MouseDoubleClick event of wpfdatagrid in the view as I'm using mvvm and Prism 2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈下烟灰 2024-09-08 03:35:38

我更喜欢添加 MouseDoubleClickBehaviour,然后您可以将其附加到任何控件,该控件将绑定到您的 ViewModel。从视图的代码隐藏调用命令会创建我不喜欢的直接依赖关系。

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 中执行此操作

<ListView ...
     behaviours:MouseDoubleClickBehaviour.Command="{Binding Path=ItemSelectedCommand}"
     behaviours:MouseDoubleClickBehaviour.CommandParameter="{Binding ElementName=txtValue, Path=Text}"
 .../>

I prefer adding a MouseDoubleClickBehaviour and then you can attach it to any control, which will bind to your ViewModel. Calling commands from the View's code-behind creates direct dependencies which I don't like.

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);
    }
}

Then you can do this in your XAML

<ListView ...
     behaviours:MouseDoubleClickBehaviour.Command="{Binding Path=ItemSelectedCommand}"
     behaviours:MouseDoubleClickBehaviour.CommandParameter="{Binding ElementName=txtValue, Path=Text}"
 .../>
各空 2024-09-08 03:35:38

在 View 的代码隐藏中侦听 MouseDoubleClick 事件,并在 ViewModel 上调用适当的方法:

public class MyView : UserControl 
{
    ...

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

    private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ViewModel.OpenSelectedItem();
    }

Listen to the MouseDoubleClick event in the code-behind of the View and call the appropriate method on the ViewModel:

public class MyView : UserControl 
{
    ...

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

    private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ViewModel.OpenSelectedItem();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文