Ank*_*esh 5 wpf treeview lazy-loading hierarchicaldatatemplate
我想实现一个具有以下结构的树视图.....
[RootNode] <----树的根
- [ParentNode P1] <---- ModelClass P1的对象
---- [ChildNode C1] <----- ModelClass C1的对象(有不同类型的子节点) ()
[ChildNode C2] <----- ModelClass C2的对象(也有不同类型的子
节点)---- [ChildNode C3] <----- ModelClass C3的对象(有)不同类型的孩子)
- [ParentNode Q1] <---- ModelClass Q1的对象
---- [ChildNode B1] <----- ModelClass B1的对象(也有不同类型的孩子)
- --- [ChildNode B2] <----- ModelClass B2的对象(也有不同类型的子
节点)---- [ChildNode B3] <----- ModelClass B3的对象(有不同类型的子节点)()
[ParentNode R1] <---- ModelClass R1的对象
---- [ChildNode A1] <----- ModelClass A1的对象(也有不同类型的子节点)
---- [ ChildNode A2] <----- ModelClass A2的对象(也有不同类型的子
节点)---- [ChildNode A3] <----- ModelClass A3的对象(也有不同类型的子节点))
我已经查看了本网站以及网络上提出的许多解决方案.....但只是无法弄清楚如何做到这一点......
这是我对Wpf的第一次尝试,这是一个至关重要的要求......
也很难找到上述不同类的对象模型.....
上面显示的所有类都有其他属性,包括它们的子节点......我不想只显示子节点的所有属性
完全不解......看到不同的解决方案
如果我能在这方面得到一些帮助,真的很棒......
谢谢
如果您使用自定义集合,则分层数据模板可以工作....我创建了这样的类:
public class EntityBase :ObservableCollection<object>
{
}
public class Parent : EntityBase
{
}
public class ChildA : EntityBase // Dont make it a collection if it has noe childern to be displayed so dont inherit for EntityBase
{
//Child Properties
}
public class ChildB : EntityBase
{
//Child Properties
}
Run Code Online (Sandbox Code Playgroud)
现在,当你最终将数据绑定到TreeView时,你将把items ChildA和ChildBitems作为Parent对象的子项,即
public ObservableCollection<object> GetData()
{
var temp = new ObservableCollection<object>();
Parent parent = new Parent(); // Root Node
temp.Add(parent);
parent.Add(new ChildA()); // ChildA as Child1 of Parent
parent.Add(new ChildA()); // ChildA as Child2 of Parent
parent.Add(new ChildB()); // ChildB as Child3 of Parent
parent.Add(new ChildB()); // ChildB as Child4 of Parent
return temp;
}
Run Code Online (Sandbox Code Playgroud)
最后,Hierarchial数据模板看起来像..
<TreeView Name="test" Grid.Row="0" ItemsSource="{Binding Path=TreeData,Source={StaticResource ResourceKey=DataSource}}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type EntityLayer:Parent}" ItemsSource="{Binding}">
<StackPanel>
<TextBlock>Parent</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type EntityLayer:ChildA}" ItemsSource="{Binding}">
<StackPanel>
<TextBlock Text="{Binding Path = Name}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type EntityLayer:ChildB}" ItemsSource="{Binding}">
<StackPanel>
<TextBlock Text="{Binding Path = Name}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7945 次 |
| 最近记录: |