zoukankan      html  css  js  c++  java
  • WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView

    Dictionary中的<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>


    Jobs jobs1 = new Jobs();

    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 class Job

    {

        public int Id

        { get; set; }

        public string Name

        { get; set; }

        public string Status

        { get; set; }

    }
    public class Jobs : System.Collections.ObjectModel.ObservableCollection<Job> { }

  • 相关阅读:
    Objective-C 在Categroy中创建属性(Property)
    iOS截屏
    iOS简易图片选择器 (图片可多选,仿微信)
    iOS 3D touch 使用技巧
    soap request by afnetworking2.X/3.X
    类似网易新闻 title栏 滚动时 文字放大&变色
    iOS 用collectionview 做的无限图片滚动 广告banner适用
    iOS WebP转换工具
    微博app中常用正则表达式
    python中property(lambda self: object())简单解释
  • 原文地址:https://www.cnblogs.com/sjqq/p/6951587.html
Copyright © 2011-2022 走看看