右键单击在TreeView中创建一个子节点

时间:2015-03-04 17:46:34

标签: c# wpf xaml mvvm treeview

这是我设置的代码,用于在我的代码中从Tree数据结构中绑定TreeView:

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

在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>

这适用于获取左键单击的项目,但我如何获得正确的单击项目以便我可以显示上下文菜单并允许用户在树中的该级别添加/修改子节点?

1 个答案:

答案 0 :(得分:1)

请参阅下面给出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));
}

参考:Select TreeView Node on right click before displaying ContextMenu