将对象绑定到 WPF TreeView

2023-11-29

我想知道如何将自定义数据类型绑定到TreeView.

数据类型基本上是包含其他数组列表的对象数组列表。访问看起来像这样:

foreach (DeviceGroup dg in system.deviceGroups)
    {
        foreach (DeviceType dt in dg.deviceTypes)
        {
            foreach (DeviceInstance di in dt.deviceInstances)
            {

            }
        }
    }

我想要TreeView看起来像这样:

设备组1

 --> DeviceType1
      --DeviceInstance1
      --DeviceInstance2
 --> DeviceType2
      --DeviceInstance1

设备组2

 --> DeviceType1
      --DeviceInstance1
 --> DeviceType2

好的,这就是HierarchicalDataTemplate会拯救你的。诀窍是您将需要使用两个不同的层次结构模板,因为这里有一个三级层次结构。我构建了一个简单的UserControl为了显示。首先,这里是一些创建模型数据的隐藏代码,类似于您所拥有的:

public partial class ThreeLevelTreeView : UserControl
{
    public ArrayList DeviceGroups { get; private set; }

    public ThreeLevelTreeView()
    {
        DeviceInstance inst1 = new DeviceInstance() { Name = "Instance1" };
        DeviceInstance inst2 = new DeviceInstance() { Name = "Instance2" };
        DeviceInstance inst3 = new DeviceInstance() { Name = "Instance3" };
        DeviceInstance inst4 = new DeviceInstance() { Name = "Instance4" };

        DeviceType type1 = new DeviceType() { Name = "Type1", DeviceInstances = new ArrayList() { inst1, inst2 } };
        DeviceType type2 = new DeviceType() { Name = "Type2", DeviceInstances = new ArrayList() { inst3 } };
        DeviceType type3 = new DeviceType() { Name = "Type3", DeviceInstances = new ArrayList() { inst4 } };
        DeviceType type4 = new DeviceType() { Name = "Type4" };

        DeviceGroup group1 = new DeviceGroup() { Name = "Group1", DeviceTypes = new ArrayList() { type1, type2 } };
        DeviceGroup group2 = new DeviceGroup() { Name = "Group2", DeviceTypes = new ArrayList() { type3, type4 } };

        DeviceGroups = new ArrayList() { group1, group2 };

        InitializeComponent();
    }
}

public class DeviceGroup
{
    public string Name { get; set; }
    public ArrayList DeviceTypes { get; set; }
}

public class DeviceType
{
    public string Name { get; set; }
    public ArrayList DeviceInstances { get; set; }
}

public class DeviceInstance
{
    public string Name { get; set; }
}

这里没什么困难,但请注意你应该使用ObservableCollection代替ArrayList如果您想动态添加和删除集合。现在让我们看看该控件的 XAML:

<UserControl x:Class="TestWpfApplication.ThreeLevelTreeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApplication"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TreeView ItemsSource="{Binding DeviceGroups}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding DeviceTypes}">
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding DeviceInstances}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

这是结果:

替代文本http://img684.imageshack.us/img684/6281/ Threeleveltreeview.png

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将对象绑定到 WPF TreeView 的相关文章

随机推荐

  • 如何访问共享点库中的自定义列 - 图形 API

    我使用自定义列将子项列出在文档库的根目录中 当我使用 MS graph REST API 进行以下调用时 不会为子项返回任何自定义列数据 https graph microsoft com beta sites
  • Android 4.4 (KitKat) 上的 Android Gallery 返回 Intent.ACTION_GET_CONTENT 的不同 URI

    在 KitKat 之前 或者在新 Gallery 之前 Intent ACTION GET CONTENT返回一个像这样的 URI 内容 媒体 外部 图像 媒体 3951 使用ContentResolver并查询MediaStore Ima
  • 未导航到本机反应中的特定屏幕

    我是原生反应新手 我创建了两个文件 Browse js 和 Drawer js 我在 Browse js 中有一些按钮 但是当我在 Drawer js 中完成完整的 Browse js 时 像这样 gt import React Compo
  • 添加与 Ruby Gem Mailboxer 的所属关系

    我正在构建一个电子商务应用程序 并希望实现诸如消息传递系统之类的东西 在应用程序中 所有对话都将与Product模型或Order模型 在这种情况下 我想将相关对象 类型 id 我想 存储到Conversation object 要添加字段
  • 是否可以声明一个包含另一个常量数组的常量数组?

    我想做这样的事情 const MyFirstConstArray array 0 1 of string Hi Foo MySecondConstArrayWhichIncludesTheFirstOne array 0 2 of stri
  • 在 core_cm4.h 上为什么有类似 ((uint32_t)(int32_t)IRQn) 的转换?

    在 core cm4 h 的以下代码中 为什么存在双重转换 uint32 t int32 t IRQn 例如在以下函数中 STATIC INLINE void NVIC EnableIRQ IRQn Type IRQn NVIC gt IS
  • 如何根据 swing 中的 JPanel(table) 行数增加 JFrame 大小

    我有 Swing 应用程序 它执行以下操作 public void init jFrame new JFrame jFrame add sortingDataInputComponent asComponent jFrame setDefa
  • 如何在 ttk.OptionMenu 周围制作边框

    在尝试制作入口框架时 我遇到了一个问题 我无法在 ttk OptionMenu 周围制作边框以使其看起来与 ttk Entry 相似 图中是相邻的两个 制作选项菜单 option ttk OptionMenu bottom containe
  • DatabaseMetaData.getColumns 返回同义词的空结果集

    方法getColumns 元数据上的同义词返回空结果集 对于表和视图 它正确返回列列表 这种情况发生在 Oracle 11g Express 并使用最新的 Oracle JDBC 驱动程序 11 2 3 上 其他 SQL 服务器也会发生这种
  • 我们如何在 SwiftUI 中访问 List 的 DisclosureGroup?

    我使用此代码在 SwiftUI 2 0 和 macOs 10 15 7 的列表中显示我的父母和孩子数据 默认情况下 我的父母处于折叠状态 我喜欢强制其中一些通过我的按钮操作进行扩展 我有那个按钮 有谁知道如何解决这个问题 这是我的代码 im
  • 设置 DAY_OF_WEEK 返回意外结果

    我想将给定日历实例的时间戳设置为一周的开始 星期一 相反 它返回一个看似完全不相关的时间戳 除非我在这样做之前访问日历的任何字段 我在下面提供了一个示例 另请参阅此可运行示例Ideone 这是预期的行为吗 这背后的逻辑是什么 是的 我听说过
  • !important 已覆盖

    我正在使用 JQuery mobile 我希望我的应用程序上的所有文本都是紫色的 我这样做了 color 7A68AE important in my body我的 CSS 部分 然后我尝试在 firebug 中调试它并注意到我的 impo
  • PHP DomDocument 更改条件注释

    我有这个带有条件注释的 html 文件
  • 在搜索结果中显示整行

    在 Visual Studio Code vscode 中 当我搜索某些内容时 每个搜索结果中的行开头可能会被切断 怎样才能看到被剪掉的部分呢 例如 假设我搜索 directory 搜索结果可能会显示以下内容 require directo
  • 如何期待 Robotium 中的异常?

    这是我的测试用例 public void testStartActivityWithoutExtraData try getActivity Assert fail Should have thrown IllegalStateExcept
  • perl删除连续的重复行

    我想删除连续的重复行 即例如 test txt car speed is good bike slower than car plane super fast super fast bullet train super fast 这将删除除
  • Firebase 数据库结构

    我目前正在为 iOS 创建一个电子商务应用程序 但我无法决定如何为用户通过 关键字 搜索项目的场景构建数据库 我不确定存储关键字数组或仅存储该项目的关键字字符串是否会更好 如果大家有什么建议请告诉我 谢谢 这是我现在的单个项目的结构 ite
  • Ajax.BeginForm 导致重定向到部分视图而不是就地视图

    我的 Search cshtml 有一个名为 search results 的 div 需要更新 SearchResults 是操作名称 我已经在 MVC2 VS2008 项目上做过很多次了 但这是我第一次使用 MVC3 和 VS2010
  • AI Platform 中的 PyTorch 模型部署

    我正在 Google Cloud AI Platform 中部署 Pytorch 模型 出现以下错误 ERROR gcloud beta ai platform versions create Create Version failed B
  • 将对象绑定到 WPF TreeView

    我想知道如何将自定义数据类型绑定到TreeView 数据类型基本上是包含其他数组列表的对象数组列表 访问看起来像这样 foreach DeviceGroup dg in system deviceGroups foreach DeviceT