Treeview双击事件

时间:2012-03-21 13:13:31

标签: wpf events mvvm treeview

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

<TreeView x:Name="tvTest" ItemsSource="{Binding}" Style="{StaticResource TreeviewStyle}" MouseDoubleClick="tvTest_MouseDoubleClick">

在视图代码隐藏中生成eventhandler。我知道这可能不是最优雅的方式,但是由于treeview缺少一个命令对象,我现在会坚持这个:

Public Sub tvTest_MouseDoubleClick(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs)

End Sub

在第一个视图中,这是正确的,但第二个视图给出了这个错误:
* tvTest_MouseDoubleClick不是MySecondView的成员。*

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

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

此致

米歇尔

修改
投票给Alex的解决方案。但是,为了解决一般问题,我使用了http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/

1 个答案:

答案 0 :(得分:1)

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

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

这是我写的一个基本类,用于将DoubleClickCommand公开给任何Framework元素

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

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