WPF中TreeView设置鼠标右键菜单

2019年09月30日 60点热度 0人点赞 0条评论

1.设置TreeView对象,代码

<Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
       
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TreeView x:Name="treeView" PreviewMouseRightButtonDown="TreeView_PreviewMouseRightButtonDown">
            <TreeViewItem Header="节点1" Name="node1"/>
            <TreeViewItem Header="节点2" Name="node2" IsExpanded="True">
                <TreeViewItem Header="[节点2[一级子菜单1" Name="node21"/>
                <TreeViewItem Header="[节点2]一级子菜单2" Name="node22">
                    <TreeViewItem Header="{}{[节点2]一级子菜单2}二级子菜单1" Name="node221"/>
                    <TreeViewItem Header="{}{[节点2]一级子菜单2}二级子菜单2" Name="node222"/>
                </TreeViewItem>
                <TreeViewItem Header="[节点2]一级子菜单3" Name="node23"/>
                <TreeViewItem Header="[节点2]一级子菜单4" Name="node24"/>
            </TreeViewItem>

        </TreeView>
        <GridSplitter Width="5" Background="#FF3768EA"></GridSplitter>
    </Grid>
</Window>

界面预览

2.菜单注册事件为 PreviewMouseRightButtonDown事件,事件代码如下

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace WpfApp1
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void TreeView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            if (treeViewItem != null)
            {
                treeViewItem.Focus();
                e.Handled = true;

                ContextMenu contextMenu = new ContextMenu();
                MenuItem menuItem = new MenuItem() { Header = treeViewItem.Header };
                menuItem.Click += delegate { MessageBox.Show("右键菜单,选中节点为[" + treeViewItem.Header + "]"); };
                contextMenu.Items.Add(menuItem);

                if (treeViewItem.Name == "node221")
                {
                    menuItem = new MenuItem() { Header = treeViewItem.Header + "专用功能" };
                    menuItem.Click += delegate { MessageBox.Show("专用功能,\r\n\r\n选中节点为[" + treeViewItem.Header + "]"); };
                    contextMenu.Items.Add(menuItem);
                }
                else if (treeViewItem.Name == "node21")
                {
                    menuItem = new MenuItem() { Header = treeViewItem.Header + "专用右键菜单" };
                    menuItem.Click += delegate { MessageBox.Show("专用右键菜单,\r\n\r\n选中节点为[" + treeViewItem.Header + "]"); };
                    contextMenu.Items.Add(menuItem);
                }

                this.treeView.ContextMenu = contextMenu;

            }
        }

        static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }
    }
}

3.运行结果

Kratos

保持饥渴的专注,追求最佳的品质