WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView
Posted lonelyxmas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView相关的知识,希望对你有一定的参考价值。
原文:WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeViewDictionary<string, CustomeType>中的CustomeType是一个集合,将其绑定生成一棵树,树的第一层节点是Dictionary的Key,第二层是CustomeType集合,所有代码用XAML实现。
代码如下:
<TreeView Name="dictree" ItemsSource="{Binding}">
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Value}" DataType="{x:Type local:Job}">
<TextBlock Text="{Binding Id}"><TextBlock Text="{Binding Name}"></TextBlock></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Value}">
<TextBlock Text="{Binding Key}"></TextBlock>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
jobs1.Add(new Job() { Id = 1, Name = "CEO1"}); jobs1.Add(new Job() { Id = 2, Name = "CEO2"}); jobs1.Add(new Job() { Id = 3, Name = "CEO3"}); Jobs jobs2 = new Jobs(); jobs2.Add(new Job() { Id = 1, Name = "CEO1"}); jobs2.Add(new Job() { Id = 2, Name = "CEO2"}); jobs2.Add(new Job() { Id = 3, Name = "CEO3"}); Dictionary<string, Jobs> allJob = new Dictionary<string, Jobs>(); allJob.Add("CEO one", jobs1); allJob.Add("CEO two", jobs2); dictree.DataContext = allJob; { public int Id { get; set; } public string Name { get; set; } public string Status { get; set; } } public class Jobs : System.Collections.ObjectModel.ObservableCollection<Job> { }
Jobs jobs1 = new Jobs();
public class Job
以上是关于WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView的主要内容,如果未能解决你的问题,请参考以下文章