Wpf Treeview双击事件

Wpf Treeview双击事件,wpf,events,mvvm,treeview,Wpf,Events,Mvvm,Treeview,我有两个带有treeview控件的视图。在两个XAML文件中,我都添加了双击事件: <TreeView x:Name="tvTest" ItemsSource="{Binding}" Style="{StaticResource TreeviewStyle}" MouseDoubleClick="tvTest_MouseDoubleClick"> 在第一个视图中,这是正确的,但是第二个视图给了我这个错误: *tvTest_MouseDoubleClick不是MySecondView

我有两个带有treeview控件的视图。在两个XAML文件中,我都添加了双击事件:

<TreeView x:Name="tvTest" ItemsSource="{Binding}" Style="{StaticResource TreeviewStyle}" MouseDoubleClick="tvTest_MouseDoubleClick">
在第一个视图中,这是正确的,但是第二个视图给了我这个错误:
*tvTest_MouseDoubleClick不是MySecondView的成员*

为什么会这样?设计器生成的代码中出现错误:

AddHandler Me.tvTest.MouseDoubleClick, New System.Windows.Input.MouseButtonEventHandler(AddressOf Me.tvTest_MouseDoubleClick)
问候,

米歇尔

编辑:

投票赞成亚历克斯的解决方案。但是,为了解决一般问题,我使用了

在第二个视图中似乎没有事件处理程序(这就是为什么不建议使用代码隐藏)

我知道你说过TreeView没有双击命令,但这不能阻止我们为自己创建一个

下面是我编写的一个基本类,用于向任何框架元素公开DoubleClickCommand

    public class DoubleClickCommand
    {
        public static object GetDoubleClickParameter(DependencyObject obj)
        {
            return (object)obj.GetValue(DoubleClickParameterProperty);
        }

        public static void SetDoubleClickParameter(DependencyObject obj, object value)
        {
            obj.SetValue(DoubleClickParameterProperty, value);
        }

        public static ICommand GetDoubleClickCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(DoubleClickCommandProperty);
        }

        public static void SetDoubleClickCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(DoubleClickCommandProperty, value);
        }


        public static readonly DependencyProperty DoubleClickParameterProperty = DependencyProperty.RegisterAttached("DoubleClickParameter", typeof(object), typeof(DoubleClickCommand), new UIPropertyMetadata(null));

        public static readonly DependencyProperty DoubleClickCommandProperty = DependencyProperty.RegisterAttached("DoubleClickCommand", typeof(ICommand), typeof(DoubleClickCommand), new UIPropertyMetadata(null, OnDoubleClickCommandChanged));


        private static void OnDoubleClickCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            FrameworkElement elem = d as FrameworkElement;
            var newCommand = args.NewValue as ICommand;
            if (elem != null)
            {
                if (newCommand != null)
                {
                    elem.MouseLeftButtonDown += elem_MouseLeftButtonDown;
                }
                else
                {
                    elem.MouseLeftButtonDown -= elem_MouseLeftButtonDown;
                }
            }
        }

        private static void elem_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount > 1)
            {
                DependencyObject dep = sender as DependencyObject;
                ICommand command = GetDoubleClickCommand(dep) as ICommand;
                var parameter = GetDoubleClickParameter(dep);
                if (command != null)
                {
                    if (command.CanExecute(parameter))
                    {
                        command.Execute(parameter);
                    }
                }
            }
        }
    }

要将其用于TreeView项目,只需在TreeView的ItemTemplate上设置Command和CommandParameter(可选)。

我有两个带有TreeView控件的视图。是什么意思?我只为
TreeView
编写了一个代码段,而不是两个。这意味着我有两个几乎相同的TreeView xaml(其他命名)视图。出于这个原因,我没有在代码中添加两个片段。非常有趣。我会将代码转换为vb.net,尝试一下,然后告诉您。typeof(行为)应该是typeof(DoubleClickCommand),对吗?是的,很抱歉,我从Behaviors重命名了类,但忘记更改属性。。。将更新。我想您可以使用
UIElement
    public class DoubleClickCommand
    {
        public static object GetDoubleClickParameter(DependencyObject obj)
        {
            return (object)obj.GetValue(DoubleClickParameterProperty);
        }

        public static void SetDoubleClickParameter(DependencyObject obj, object value)
        {
            obj.SetValue(DoubleClickParameterProperty, value);
        }

        public static ICommand GetDoubleClickCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(DoubleClickCommandProperty);
        }

        public static void SetDoubleClickCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(DoubleClickCommandProperty, value);
        }


        public static readonly DependencyProperty DoubleClickParameterProperty = DependencyProperty.RegisterAttached("DoubleClickParameter", typeof(object), typeof(DoubleClickCommand), new UIPropertyMetadata(null));

        public static readonly DependencyProperty DoubleClickCommandProperty = DependencyProperty.RegisterAttached("DoubleClickCommand", typeof(ICommand), typeof(DoubleClickCommand), new UIPropertyMetadata(null, OnDoubleClickCommandChanged));


        private static void OnDoubleClickCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            FrameworkElement elem = d as FrameworkElement;
            var newCommand = args.NewValue as ICommand;
            if (elem != null)
            {
                if (newCommand != null)
                {
                    elem.MouseLeftButtonDown += elem_MouseLeftButtonDown;
                }
                else
                {
                    elem.MouseLeftButtonDown -= elem_MouseLeftButtonDown;
                }
            }
        }

        private static void elem_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount > 1)
            {
                DependencyObject dep = sender as DependencyObject;
                ICommand command = GetDoubleClickCommand(dep) as ICommand;
                var parameter = GetDoubleClickParameter(dep);
                if (command != null)
                {
                    if (command.CanExecute(parameter))
                    {
                        command.Execute(parameter);
                    }
                }
            }
        }
    }