一、原代码:
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}" Background="White" Style="{DynamicResource LabelStyle1}" BorderThickness="0" VerticalContentAlignment="Top">
<!-- 右键菜单 -->
<Label.ContextMenu>
<ContextMenu StaysOpen="true" ItemsSource="{Binding Path = Menus}"/>
</Label.ContextMenu>
</Label>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
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(){...};
}
}
错误信息:“在类型为 Project_TreeNodes 的对象上找不到 Menus 属性。 ”
二、分析:
1、原因:
2、修改:
标签:set,string,ContextMenu,get,System,using,WPF,public,模板 From: https://www.cnblogs.com/qq2806933146xiaobai/p/17039678.html