WPF TreeView 不会相应地应用 DataTemplate [英] WPF TreeView does not apply DataTemplate accordingly

查看:15
本文介绍了WPF TreeView 不会相应地应用 DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含复合结构的业务对象项目:

I have a business object project, which contains composite structure:

public class Tree 
{ public IProductComponent TreeRoot { get; set; } }

public interface ITreeComponent 
{ public string Name { get; set; } }

public class ContainerComponent : ITreeComponent
{ public BindingList<ITreeComponent> Children { get; set; } }

public class LeafComponent : ITreeComponent
{ }

我需要将此结构绑定到我的 WPF 项目中的 TreeView.先上树视图:

I need to bind this structure to a TreeView in my WPF project. The tree view first:

<TreeView x:Name="treeView" Grid.ColumnSpan="2">
 <TreeView.Resources>
  <HierarchicalDataTemplate 
   ItemsSource="{Binding Children}" 
DataType="{x:Type businessObjects:ContainerComponent}">
   <Label Content="{Binding Name}"/>
  </HierarchicalDataTemplate>
  <DataTemplate DataType="{x:Type businessObjects:LeafComponent}">
   <Label Content="{Binding Name}"/>
  </DataTemplate>
 </TreeView.Resources>
</TreeView>

以及绑定代码:

bTreeView = new Binding();
bTreeView.Source = MyTree;
bTreeView.Path = new PropertyPath("TreeRoot.Children");
treeView.SetBinding(TreeView.ItemsSourceProperty, bTreeView);

问题是 TReeView 实际上并没有使用这些模板(它只显示层次结构的顶层并调用 .ToString() 来显示这些项目.请告诉我我哪里出错了.否则,如果我设置它正在工作,但我无法在那里定义两个模板.

The problem is that the TReeView does not actually use those templates (it displays only the top level of hierarchy and calls .ToString() to display those items. Please tell me where have I gone wrong. Otherwise, if I set the it is working, but I cannot define two templates there.

谢谢.

推荐答案

我的不好 - 主程序集将 dll 与实体加载了两次而不是一次.这导致它变得疯狂 - 一旦我修复它并在问题消失后加载程序集.

My bad - the Main assembly was loading the dll with entities two times instead of one. That caused it to go crazy - as soon as I fixed it and the assembly loaded once the problems went away.

这篇关于WPF TreeView 不会相应地应用 DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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