【问题标题】:WPF TreeView, Entity Framework and HierarchicalDataTemplate with nested child lists带有嵌套子列表的 WPF TreeView、实体框架和 HierarchicalDataTemplate
【发布时间】:2023-03-28 22:24:01
【问题描述】:

我正在尝试使用 HierarchicalDataTemplate(s) 将复杂的数据结构绑定到 WPF TreeView。数据集合作为 MyObject 的 IList 存储在我的 ViewModel 中 - MyObject 有几个属性,其中一些属性本身就是列表。

我想要实现的输出类似于:

+ MyObject 1 <br>
  + List1 <br>
    - List 1 Object 1 <br>
    - List 1 Object 2 <br>
  + List2 <br>
    - List 2 Object 1 <br>
    - List 2 Object 2 <br>
+ MyObject 2 <br>
  + List1 <br>
    - List 1 Object 1 <br>
    - List 1 Object 2 <br>
  + List2 <br>
    - List 2 Object 1 <br>
    - List 2 Object 2 <br>

但是我似乎无法让我看到的复合集合提到一些工作的地方 -

【问题讨论】:

    标签: wpf frameworks treeview entity hierarchicaldatatemplate


    【解决方案1】:

    我刚刚做了类似的事情。不幸的是,您不能直接执行此操作,因为 TreeViewItem 只接受一个集合作为其 ItemsSource。

    我所做的是创建一个模型,以 TreeView 所需的方式公开内容。

    public class MyObjectWrapper
    {
      public MyObject Target {get;set;}
      public IEnumerable MyLists
      {
        get
        {
          yield return Target.List1;
          yield return Target.List2;
        } 
      }
    }
    

    其中 MyObject 定义为:

    public class MyObject
    {
        public List1CollectionType List1 {get;private set;}
        public List2CollectionType List2 {get;private set;}
    }
    

    你的绑定然后

    • TreeViewItem : MyObjectWrapper
      • ItemsSource : MyLists
      • TreeViewItem:List1CollectionType
        • ItemsSource : {Binding}(直接绑定到数据上下文)
      • TreeViewItem:List2CollectionType
        • ItemsSource:{Binding}

    您需要一个用于 MyObjectWrapper、List1CollectionType 和 List2CollectionType 的 DataTemplate。

    【讨论】:

    • 您能澄清一下Target 指的是什么吗?感谢您再次访问。
    • @UB3571 目标是包含要在树视图中显示的列表的假设对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    相关资源
    最近更新 更多