一、原代码:

  XAML:
<TreeView x:Name="trvwProjectList"  Margin="0,0,0,0" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" BorderBrush="DarkGray" BorderThickness="0.7">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type vModelLocal:Project_TreeNodes}" ItemsSource="{Binding ChildNodes}">
                        <!-- MouseRightButtonDown="ProjectListMenu_MouseRightButtonDown" -->
                        <StackPanel Orientation="Horizontal" Tag="{Binding NodeName}" MouseLeftButtonDown="ProjectListMenu_MouseLeftButtonDown" VerticalAlignment="Stretch" Margin="0">
                            <Label Content="{Binding NodeName}" Tag="{Binding NodeID}" ToolTip="{Binding NodeDescribe}" Style="{DynamicResource LabelStyle1}" BorderThickness="0" VerticalContentAlignment="Top">
                                <!-- 右键菜单 -->
                                <Label.ContextMenu>
                                    <ContextMenu StaysOpen="true" ItemsSource="{Binding Path = Menus}"/>
                                </Label.ContextMenu>
                            </Label>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ProjectViewModel
{
    /// <summary>
    /// Tree列表
    /// </summary>
    public class Project_TreeNodes
    {
        /// <summary>
        /// 节点Id
        /// </summary>
        public int NodeID { get; set; }
        /// <summary>
        /// 节点名
        /// </summary>
        public string NodeName { get; set; } = string.Empty;

        /// <summary>
        /// 节点描述
        /// </summary>
        public string NodeDescribe { get; set; } = string.Empty;

        /// <summary>
        /// 父节点Id
        /// </summary>
        public int ParentID { get; set; }

        /// <summary>
        /// 图标
        /// </summary>
        public string Img { get; set; };

        /// <summary>
        /// 右键菜单
        /// </summary>
        public ContextMenu Menus=new();

        /// <summary>
        /// 子元素
        /// </summary>
        public List<Project_TreeNodes> ChildNodes { get; set; } = new();
    }
    
    public 页面代码(){
          trvwProjectList.ItemsSource = new Project_TreeNodes(){...};
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
错误信息:“在类型为 Project_TreeNodes 的对象上找不到 Menus 属性。 ”

二、分析:

1、原因:

  HierarchicalDataTemplate模板问题,ContextMenu可以放在TreeView中,用鼠标事件选择展开内容

2、修改方式一:

  使用别人模板,但是不能对展开内容加以控制;连接:https://blog.51cto.com/u_15693505/5409852。

3、修改方式二:

  XML:

<TreeView x:Name="trvwProjectList" MouseDown="TrvwProjectList_MouseDown" PreviewMouseRightButtonDown="TrvwProjectList_PreviewMouseRightButtonDown"  Margin="0,0,0,0" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" BorderBrush="DarkGray" BorderThickness="0.7">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type vModelLocal:Project_TreeNodes}" ItemsSource="{Binding ChildNodes}">
                        <!-- MouseRightButtonDown="ProjectListMenu_MouseRightButtonDown" -->
                        <StackPanel Orientation="Horizontal" Tag="{Binding NodeName}" MouseLeftButtonDown="ProjectListMenu_MouseLeftButtonDown" VerticalAlignment="Stretch" Margin="0">
                            <Image Margin="0" Width="18" Height="16" Source="{Binding Img}"/>
                            <Label Content="{Binding NodeName}" Tag="{Binding NodeID}" ToolTip="{Binding NodeDescribe}" Style="{DynamicResource LabelStyle1}" BorderThickness="0" VerticalContentAlignment="Top"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

   C#:

/// <summary>
        /// 右键菜单
        /// </summary>
        private void TrvwProjectList_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource is DependencyObject source)
            {
                while (source != null && source.GetType() != typeof(StackPanel))
                {
                    source = System.Windows.Media.VisualTreeHelper.GetParent(source);
                }  // 找到节点名

                if (source is StackPanel item)
                {
                    CreateContextMenu(item.Tag?.ToString() ?? string.Empty);
                }
            }
        }

        /// <summary>
        /// 显示右键菜单
        /// </summary>
        /// <param name="positionItem">功能节点名</param>
        private void CreateContextMenu(string nodeName)
        {
            trvwProjectList.ContextMenu = null;
            ContextMenu contextMenu = new();  // 右键菜单对象
            MenuItem menuItem = new();
            MenuItem menuItem1 = new();
            bool isDone = false; // 是否已处理
            
            // 根节点
            switch (nodeName)
            {
                case "菜单1":
                    menuItem = new();
                    menuItem.Header = "添加";
                    {  
                       
                        menuItem1 = new();
                        menuItem1.Header = "添加组";
                        menuItem1.Click += new RoutedEventHandler((sender, e) => { MessageBox.Show("添加了组"); });
                        menuItem.Items.Add(menuItem1);

                        menuItem1 = new();
                        menuItem1.Header = "添加文件";
                        menuItem1.Click += new RoutedEventHandler((sender, e) => { MessageBox.Show("添加了文件"); });
                        menuItem.Items.Add(menuItem1);
                    }
                    contextMenu.Items.Add(menuItem);
                    contextMenu.Items.Add(new Separator());
                    menuItem = new();
                    menuItem.Header = "剪切";
                    menuItem.Click += new RoutedEventHandler((sender, e) => { MessageBox.Show("剪切"); });
                    contextMenu.Items.Add(menuItem);

                    menuItem = new();
                    menuItem.Header = "复制";
                    menuItem.Click += new RoutedEventHandler((sender, e) => { MessageBox.Show("复制"); });
                    contextMenu.Items.Add(menuItem);

                    trvwProjectList.ContextMenu = contextMenu;
                    isDone = true;
                    break;
                default:
                    break;
            }
       
            // 组节点名
            if (isDone) { }
            else if (nodeName.Contains("组"))
            {

            }
            else
            {

            }
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.

 

 

作者:꧁执笔小白꧂