wpf - 用不同的父节点和不同的子节点实现 WPF TreeView ?

标签 wpf treeview lazy-loading hierarchicaldatatemplate

我想实现一个具有以下结构的 TreeView .....

[RootNode] <---- 树的根
--[ParentNode P1] <---- ModelClass P1 的对象
----[ChildNode C1] <----- ModelClass C1的对象(也有不同类型的 child )
----[ChildNode C2] <----- ModelClass C2 的对象(也有不同类型的 child )
----[ChildNode C3] <----- ModelClass C3 的对象(也有不同类型的 child )
--[ParentNode Q1] <---- ModelClass Q1 的对象
----[ChildNode B1] <----- ModelClass B1 的对象(也有不同类型的 child )
----[ChildNode B2] <----- ModelClass B2 的对象(也有不同类型的 child )
----[ChildNode B3] <----- ModelClass B3 的对象(也有不同类型的 child )
--[ParentNode R1] <---- ModelClass R1 的对象
----[ChildNode A1] <----- ModelClass A1 的对象(也有不同类型的 child )
----[ChildNode A2] <----- ModelClass A2 的对象(也有不同类型的 child )
----[ChildNode A3] <----- ModelClass A3 的对象(也有不同类型的 child )

我已经查看了本网站以及网络上提出的许多解决方案......但只是无法弄清楚如何去做......

这是我对 Wpf 的第一次尝试,这是一个至关重要的要求......

也很难为上述不同的类制作对象模型.....

上面显示的所有类还有其他属性,包括它们的子节点......
我不想只显示子节点的所有属性

完全被...看到不同的解决方案感到困惑

如果我能在这方面得到一些帮助,那就太好了……

谢谢

最佳答案

如果您使用自定义集合,分层数据模板可以工作......
我的类(class)是这样的:

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
}  

现在,当您最终将数据绑定(bind)到 TreeView 时,您将制作 ChildAChildB作为 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;

    }  

最后层次数据模板看起来像..
<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 - 用不同的父节点和不同的子节点实现 WPF TreeView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6045742/

相关文章:

wpf - 将菜单项添加到 FlowDocumentReader ContextMenu

.net - WPF 生产准备好了吗?

winforms - 是否可以在不使用 ImageList 的情况下将图像添加到 TreeView 节点?

java - 延迟加载展示 primefaces

Angular - 延迟加载谷歌地图 - "You have included the Google Maps JavaScript API multiple..."

c++ - 延迟加载可以被认为是 RAII 的一个例子吗?

c# - 使用 DisplayMemberPath 将自定义类数据绑定(bind)到 WPF ComboBox

c# - 在 wpf 中禁用 itemscontrol 上的鼠标滚轮

javascript - 如何在angularJS中解析Json字符串?

c# - 简单的 WPF 绑定(bind)不起作用