C#学习教程:wpf Treeview有三个级别分享


wpf Treeview有三个级别

我是初学者,并尝试创建我的第一个应用程序,其中包含一个有三个级别的treeView
第一级显示进程的名称
第二级显示其修复的模块和线程
第三级包含子模块和子线程

proc1 Module subMod1 Thread thread1 proc2 Module submod1 Thread subThread 

这是xmal.cs文件

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); for (int i = 0; i <= 3; i++) { Module mod = new Module { modulelist = new List() { new moduleList { Name = "mod1" } , new moduleList { Name = "mod1" } } }; Thread th = new Thread { threadlist = new List() { new threadList { Name = "thread1" } , new threadList { Name = "thread1" } } }; ProcName proc1 = new ProcName { Name = "proc" + i, mod = new Module(), th = new Thread() }; tv.DataContext = proc1; } } public class ProcName { public string Name { get; set; } public Module mod = new Module(); public Thread th = new Thread(); } public class Module { public string Name = "Module"; public List modulelist { get; set; } } public class moduleList { public string Name { get; set; } } public class Thread { public string Name = "Thread"; public List threadlist { get; set; } } public class threadList { public string Name { get; set; } } } 

这是xmal文件

                        </      

我知道这不是正确的方法,但试图像这样做
如何正确使用?

您可以使用Interface来实现此function。

xaml.cs

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var subNodes = new List { new SubNode { Name = "Sub Node 1" }, new SubNode { Name = "Sub Node 2" }, new SubNode { Name = "Sub Node 3" }, new SubNode { Name = "Sub Node 4" } }; var nodes = new List { new Thread { Name = "Thread 1", ChildNodes = subNodes }, new Thread { Name = "Thread 2", ChildNodes = subNodes }, new Module { Name = "Module 1", ChildNodes = subNodes }, new Module { Name = "Module 2", ChildNodes = subNodes } }; var processes = new List { new Process{ Name = "Process1", ChildNodes = nodes }, new Process{ Name = "Process2", ChildNodes = nodes } }; TreeView.ItemsSource = processes; } } public interface ITreeNode { string Name { get; set; } List ChildNodes { get; set; } } public class Process : ITreeNode { public string Name { get; set; } public int ID { get; set; } public List ChildNodes { get; set; } } public class Module : ITreeNode { public string Name { get; set; } public List ChildNodes { get; set; } } public class Thread : ITreeNode { public string Name { get; set; } public int ID { get; set; } public List ChildNodes { get; set; } } public class SubNode : ITreeNode { public string Name { get; set; } public List ChildNodes { get => null; set => throw new System.NotImplementedException(); } } 

XAML

如果您不需要不同的级别模板,则可以使用

        

如果你需要不同的级别模板,你可以使用这样的东西,

上述就是C#学习教程:wpf Treeview有三个级别分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

                       

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/960108.html

发表评论

邮箱地址不会被公开。 必填项已用*标注