使用WPF HierarchicalDataTemplate使用接口的任何方法 [英] Any way to use interfaces with wpf HierarchicalDataTemplate

查看:98
本文介绍了使用WPF HierarchicalDataTemplate使用接口的任何方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用接口IFooNode,它是树的一部分.
我想使用HierarchicalDataTemplate在TreeView中显示这棵树. 但是,由于界面的原因,此方法不起作用. 我发现有两种方法都不能被我称为很好"

Hi I am using an interface IFooNode, which is part of a tree.
I wanted to display this tree in a TreeView using a HierarchicalDataTemplate. This however does not work due to the interface. I see two ways around that neither are what I would call "nice"

  1. 找出真正实现IFooNode的类型(我们称其为FooNode ...),然后找到定义FooNode的程序集,添加对该程序集的引用,并为FooNode创建一个HierarchicalDataTemplate(希望实现类永不改变. ..)

  1. Find out what type really implements IFooNode (let's call it FooNode...), then find the assembly defining FooNode, add a Reference to that assembly and create a HierarchicalDataTemplate for FooNode (hoping the Implementating class never changes...)

写一个包装IFooNode的类MyCoolIFooNodeWrapperForWpf,不绑定到树,而是从树根填充MyCoolIFooNodeWrapperForWpf的列表,并为MyCoolIFooNodeWrapperForWpf创建一个HierarchicalDataTemplate.

Write a class MyCoolIFooNodeWrapperForWpf that wraps IFooNode, do not bind to the tree, but fill a list of MyCoolIFooNodeWrapperForWpf from the tree root and create a HierarchicalDataTemplate for MyCoolIFooNodeWrapperForWpf.

无论数据是什么类型,我都可以以某种方式总是应用HierarchicalDataTemplate(我认为这是次要的事情……)

Can I somehow always apply a HierarchicalDataTemplate, no matter what type the data is (This I think would be the lesser evil...)

还有其他想法吗?

推荐答案

您可以使用ItemTemplateSelector.在选择器的代码中,您可以检查数据对象实现的接口,并为此项目选择适当的DataTemplate/HeirarchicalDataTemplate.

You can use ItemTemplateSelector. In the selector's code you can check which interface your data object implements, and select the appropriate DataTemplate/HeirarchicalDataTemplate for this item.

选择器既可以应用于容器本身(ItemsControl.ItemTemplateSelector),也可以应用于层次结构数据模板(HeirarchicalDataTemplate.ItemTemplateSelector).

The selector can both be applied to the container itself (ItemsControl.ItemTemplateSelector) and to your hierarchical data template (HeirarchicalDataTemplate.ItemTemplateSelector).

我认为这是次要的邪恶.

I think this is the lesser evil.

这篇关于使用WPF HierarchicalDataTemplate使用接口的任何方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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