问题描述
我要实现的树形视图具有以下结构.....
I want to implememt a tree view with has the following structure.....
[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的对象(也具有不同类型的子代)
[RootNode] <---- Root of tree
--[ParentNode P1] <---- Object of ModelClass P1
----[ChildNode C1] <----- Object of ModelClass C1 (have children of different type as well)
----[ChildNode C2] <----- Object of ModelClass C2 (have children of different type as well)
----[ChildNode C3] <----- Object of ModelClass C3 (have children of different type as well)
--[ParentNode Q1] <---- Object of ModelClass Q1
----[ChildNode B1] <----- Object of ModelClass B1 (have children of different type as well)
----[ChildNode B2] <----- Object of ModelClass B2 (have children of different type as well)
----[ChildNode B3] <----- Object of ModelClass B3 (have children of different type as well)
--[ParentNode R1] <---- Object of ModelClass R1
----[ChildNode A1] <----- Object of ModelClass A1 (have children of different type as well)
----[ChildNode A2] <----- Object of ModelClass A2 (have children of different type as well)
----[ChildNode A3] <----- Object of ModelClass A3 (have children of different type as well)
我已经看过了本网站以及网络上提出的许多解决方案......但是却无法弄清楚该怎么做.....
I have looked at many of the solution proposed on this site as well as on the web.....but just cant figure out how to do it.....
这是我第一次尝试Wpf,这是一个关键要求……
This is my first attempt on Wpf and this is a crucial requirement ......
也很难为上述不同的类制作对象模型....
Also finding hard to make the Object Model for the above different classes .....
上面显示的所有类都具有其他"属性,包括其子节点...我不想仅显示子节点的所有属性
All the classes displayed above have Other properties as well including their child nodes...i dont want to display all properties only the child nodes
完全困惑...看到不同的解决方案
Totally puzzled by ... seeing different solution
如果我能在这方面得到一些帮助,那将是非常好的...
It would be really great if i could recieve some help in this regard...
谢谢
推荐答案
如果使用自定义集合,则层次结构数据模板将起作用.我使我的课程如下:
Hierarchial Data Templates work if you use custom collections....I made my classes like this :
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
}
现在,当您最终将数据绑定到TreeView时,您将把ChildA
和ChildB
项作为Parent
对象的子项,即
Now when you finally bind the data to you TreeView you'll make ChildA
and ChildB
items as Child items of Parent
object i.e
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;
}
最后,层次结构数据模板将看起来像..
Finally The Hierarchial data templates will look like..
<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>
这篇关于用不同的父节点以及不同的子节点实现WPF树视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!