我是初学者,并尝试创建我的第一个应用程序,其中包含一个具有三个级别的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<moduleList>() { new moduleList { Name = "mod1" } ,
new moduleList { Name = "mod1" } }
};
Thread th = new Thread
{
threadlist = new List<threadList>() { 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> modulelist
{ get; set; }
}
public class moduleList
{
public string Name
{ get; set; }
}
public class Thread {
public string Name = "Thread";
public List<threadList> threadlist
{ get; set; }
}
public class threadList
{
public string Name
{ get; set; }
}
}
这是 xmal 文件
<TreeView Name="tv" ItemsSource="{Binding}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ProcName}">
<TextBlock Margin="3" Text="{Binding Name}"/>
<!-- child node will be a MasterAction -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Module}">
<TextBlock Text="{Binding Name}"/>
<!-- leaf will be a SlaveAction -->
<HierarchicalDataTemplate.ItemTemplate ItemSource ="{Binding moduleList} ">
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Thread}">
<TextBlock Text="{Binding Name}"/>
<!-- leaf will be a SlaveAction -->
<HierarchicalDataTemplate.ItemTemplate ItemSource ="{Binding threadList} ">
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Window>
我知道这不是正确的方法,但试图像这样做 如何正确使用?
答案 0 :(得分:0)
您可以使用Interface
来实现此目的。
<强> xaml.cs 强>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var subNodes = new List<ITreeNode>
{
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<ITreeNode>
{
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<Process>
{
new Process{ Name = "Process1", ChildNodes = nodes },
new Process{ Name = "Process2", ChildNodes = nodes }
};
TreeView.ItemsSource = processes;
}
}
public interface ITreeNode
{
string Name { get; set; }
List<ITreeNode> ChildNodes { get; set; }
}
public class Process : ITreeNode
{
public string Name { get; set; }
public int ID { get; set; }
public List<ITreeNode> ChildNodes { get; set; }
}
public class Module : ITreeNode
{
public string Name { get; set; }
public List<ITreeNode> ChildNodes { get; set; }
}
public class Thread : ITreeNode
{
public string Name { get; set; }
public int ID { get; set; }
public List<ITreeNode> ChildNodes { get; set; }
}
public class SubNode : ITreeNode
{
public string Name { get; set; }
public List<ITreeNode> ChildNodes { get => null; set => throw new System.NotImplementedException(); }
}
<强> XAML 强>
如果您不需要不同的级别模板,可以使用
<TreeView x:Name="TreeView">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ChildNodes}">
<TextBlock Margin="3" Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
如果你需要不同级别的模板,你可以使用这样的东西,
<TreeView x:Name="TreeView">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Process}" ItemsSource="{Binding ChildNodes}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name, StringFormat='{}{0} '}" />
<TextBlock Text="{Binding Path=ID, StringFormat=(ID: {0})}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Module}" ItemsSource="{Binding ChildNodes}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Thread}" ItemsSource="{Binding ChildNodes}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name, StringFormat='{}{0} '}" />
<TextBlock Text="{Binding Path=ID, StringFormat=(ID: {0})}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:SubNode}" ItemsSource="{Binding ChildNodes}">
<TextBlock Text="{Binding Path=Name}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>