Michel van Engelen
Michel van Engelen

Reputation: 2879

Treeview doubleclick event

I have two views with a treeview control. In both XAML files I added a doubleclick event:

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

The eventhandler is generated in the view codebehind. I know this might not be the most elegant way, but as treeview is lacking a command object I will stick to this for now:

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

End Sub

In the first view this works correct but the second view gives me this error:
*tvTest_MouseDoubleClick is not a member of MySecondView.*

Why is this the case? The error occurs in the designer generated code:

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

Regards,

Michel

EDIT:
Voted for the Alex' solution. However, to fix things in general I used http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/

Upvotes: 0

Views: 1196

Answers (1)

AlexDrenea
AlexDrenea

Reputation: 8039

It appears that you don't have the event handler in the second view (and this is why it's not recommended to use the code behind).

I know you said that the TreeView does not have the Double Click Command but that can't stop us from creating one for ourselves.

Here's a basic class I wrote to expose a DoubleClickCommand to any Framework element

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

To use it for your TreeViewItems, just set the Command and CommandParameter(optional) on your TreeView's ItemTemplate.

Upvotes: 1

Related Questions