如何使用WPF树视图与不同类型的嵌套对象 [英] How to use the wpf treeview with nested objects of different types

查看:128
本文介绍了如何使用WPF树视图与不同类型的嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想在一个TreeView显示两个班。

I have two classes that I'd like to show in a treeview.

在我的主窗口中,我有一定的对象类型的观察的集合,称它为一个对象。
Object的包含另一种类型的对象,对象B的观察到的集合。
中的每个对象B包含对象B的可观察到的集合。

In my main window, I have an observable collection of a certain type of object, call it object a. Object a contains an observable collection of another type of object, object b. Each object b contains an observable collection of object b's.

例如,我可能有一些看起来像这样

For example, I might have something that looks like this

ListOfObjectA的

ListOfObjectA's

-ObjectA1

-ObjectA1

- ListOfObjectB的

--ListOfObjectB's

---对象b

---- ListOfObjectB的

----ListOfObjectB's

---- -ObjectB

-----ObjectB

-ObjectA2

-ObjectA2

- ListOfObjectB的

--ListOfObjectB's

---对象b

我应该我的XAML模样到TreeView绑定到类似的东西?

What should my xaml look like to bind the treeview to something like that?

很抱歉,如果我没有解释这很好,我是新来WPF。

Sorry if I'm not explaining this well, I'm new to wpf.

推荐答案

RB有正确的想法。这是WPF对模板应用到普通的.NET对象的能力变得非常强大。

RB has the right idea. This is where WPF's ability to apply templates to normal .NET objects becomes very powerful.

您需要指定两个 HierarchialDataTemplates ,一个类型的对象 A ,另一个用于键入的画线b 。事情是这样的:

You need to specify two HierarchialDataTemplates, one for objects of type a, and the other for objects of type b. Something like this:

<TreeView>
    <TreeView.Resources>
        <HierarchialDataTemplate TargetType="{x:Type local:a}">
            <!-- XAML to display objects of type a -->
        </HierarchialDataTemplate>
        <HierarchialDataTemplate TargetType="{x:Type local:b}">
            <!-- XAML to display objects of type b -->
        </HierarchialDataTemplate>
    </TreeView.Resources>
</TreeView>

您将需要为的ItemsSource 每个模板让WPF知道从哪里得到孩子从对象的财产。此外,你需要一个的xmlns 声明添加到根节点指定的对象住在命名空间(通过为代表的本地在上面的示例代码)。

You'll need to specify bindings for the ItemsSource property of each template so that WPF knows where to get child objects from. Also, you'll need to add an xmlns declaration to the root node specifying the namespace that the objects live in (represented by local in the sample code above).

这篇关于如何使用WPF树视图与不同类型的嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆