A C
A C

Reputation: 139

Create a childnode in TreeView from right click

This is the code I have set up for binding the TreeView from a Tree data structure in my code:

public class ExtendedTreeView : TreeView
{
    public ExtendedTreeView()
        : base()
    {
        this.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(___ICH);
    }

    void ___ICH(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (SelectedItem != null)
        {
            SetValue(SelectedItem_Property, SelectedItem);
        }
    }

    public object SelectedItem_
    {
        get { return (object)GetValue(SelectedItem_Property); }
        set { SetValue(SelectedItem_Property, value); }
    }
    public static readonly DependencyProperty SelectedItem_Property = DependencyProperty.Register("SelectedItem_", typeof(object), typeof(ExtendedTreeView), new UIPropertyMetadata(null));
}

With this in the xaml:

 <local:ExtendedTreeView ItemsSource="{Binding Items}" SelectedItem_="{Binding Item, Mode=TwoWay}">
 .....
    <xn:ExtendedTreeView.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Add New"/>
        </ContextMenu>
    </xn:ExtendedTreeView.ContextMenu>
 </local:ExtendedTreeView>

This works for getting the left clicked item, but I how do I get the right clicked item so that I can show a context menu and allow user to add/modify a child node at that level in the tree?

Upvotes: 0

Views: 238

Answers (1)

Ayyappan Subramanian
Ayyappan Subramanian

Reputation: 5366

Refer the below code which gives the RightClickedItem.

 public class ExtendedTreeView : TreeView
{
    public ExtendedTreeView()
        : base()
    {
        this.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(___ICH);
        this.PreviewMouseRightButtonDown += ExtendedTreeView_PreviewMouseRightButtonDown;

    }

    void ___ICH(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (SelectedItem != null)
        {
            SetValue(SelectedItem_Property, SelectedItem);
        }
    }

    void ExtendedTreeView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        TreeViewItem treeViewItem =
                  VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject);

        if (treeViewItem != null)
        {
            SetValue(RightClickedItem_Property, treeViewItem.DataContext);
            e.Handled = true;
        }
    }

    static T VisualUpwardSearch<T>(DependencyObject source) where T : DependencyObject
    {
        DependencyObject returnVal = source;

        while (returnVal != null && !(returnVal is T))
        {
            DependencyObject tempReturnVal = null;
            if (returnVal is Visual || returnVal is Visual3D)
            {
                tempReturnVal = VisualTreeHelper.GetParent(returnVal);
            }
            if (tempReturnVal == null)
            {
                returnVal = LogicalTreeHelper.GetParent(returnVal);
            }
            else returnVal = tempReturnVal;
        }

        return returnVal as T;
    }



    public object RightClickedItem_
    {
        get { return (object)GetValue(RightClickedItem_Property); }
        set { SetValue(RightClickedItem_Property, value); }
    }

    public static readonly DependencyProperty RightClickedItem_Property =
        DependencyProperty.Register("RightClickedItem_", typeof(object), typeof(ExtendedTreeView), new PropertyMetadata(null));


    public object SelectedItem_
    {
        get { return (object)GetValue(SelectedItem_Property); }
        set { SetValue(SelectedItem_Property, value); }
    }
    public static readonly DependencyProperty SelectedItem_Property = DependencyProperty.Register("SelectedItem_", typeof(object), typeof(ExtendedTreeView), new UIPropertyMetadata(null));
}

Reference:Select TreeView Node on right click before displaying ContextMenu

Upvotes: 1

Related Questions