具有有效负载实体的HierarchicalDataTemplate和多对多导航绑定问题

时间:2013-04-19 00:50:45

标签: wpf xaml binding treeview hierarchicaldatatemplate

我有两个具有Payload实体的多对多实体,如下所示:

Entity Snapshot

因此,要使用部件号MasterPartNumber.pn将MasterPartNumber中的程序集存储起来,我使用导航属性ParentBOMs,该属性由关系给出: MasterPartNumber.pnID = MasterPartsList.parentPnID。 这给了我父汇编下的所有子pnID。

要获取该程序集的子部件号,我使用由ChildPn定义的MasterPartsList.pnID = MasterPartNumber.pnID导航属性。

请注意,顶层程序集项目未在MasterPartsList中列出(它们的parentPnID为null)。

My TreeView HierarchicalDataTemplate绑定是:

<TreeView x:Name="AssemblyTreeView"
          ItemsSource="{Binding BOMItems}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:MasterPartNumber}"
                              ItemsSource="{Binding ParentBOMs.ChildPn}">
      <TextBlock Text="{Binding pn}" />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>

我认为这是正确的。 我可以单步执行调试器,看看是否为每个包含子信息的项填充了整个BOMItem导航属性(ParentBOM.ChildPn.pn)。

为什么我无法在TreeView中看到这些子属性填充?!

What I should get:

Root Assembly
--Sub Assembly
----Sub Assembly
------Child (n-levels deep)

我真正得到的是:

Root Assembly

我还需要额外的转换器吗?我是否需要进一步定义我的ObservableCollection对象包装器的“getter”?

Known possible sources of the problem:
1. Entity Framework is lazy loading, and just hasn't loaded the navigation properties I see in
   the debugger being populated. (No, set LazyLoading to false.)
2. My HierarchicalDataTemplate isn't probing for children just on the fact that it has children
   -- aka it only understands to switch the binding path when a new DataType is available, or 
   something like that. (Not likely, because I've seen HierarchcialDataTemplates for self-referencing entities of a single entity type.)

What I have right:
1. I can cascade down the binding route I told my TreeView to take in the debugger. 
    Parent `pn` is populated as well as its `ParentBOMs.ChildPn.pn`. 

请帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

无法了解预期的输出应该是什么?你能画树并张贴它。

您可能必须为要呈现的域类型创建包装器VM类型。说.. MasterPartVM。您现在可以使用2条路线的逻辑定义属性,例如儿童。您的层次结构数据模板将始终使用此属性扩展到下一级别。

这将使您在实施MasterPartVM.Children时找到合适的孩子的责任 - 您似乎已经确定了这一点。如果我误解了这个问题,请告诉我。

e.g。在链接的帖子中,您可以看到我使用了一个包装器属性来组合子组和条目来整理单个列表。

这是另一个例子

public class MyPart
    {
        public string Name { get; set; }
        public bool IsRoot { get; set; }
        public string[] ChildNames { get; set; }
        public IList<MyPart> Children { 
            get {
                if (IsRoot)
                    return ChildNames.Select(c => new MyPart { Name = c, ChildNames =new[]{c} }).ToList();
                else
                    return new List<MyPart>{new MyPart { Name = "No more children" }};
        } }
    }

MyPartsCollection = new ObservableCollection<MyPart>();
            MyPartsCollection.Add(new MyPart
            {
                Name = "Root1",
                IsRoot = true,
                ChildNames = new []{"Item1", "Item2", "Item3"}
            });
<TreeView ItemsSource="{Binding MyPartsCollection}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:MyPart}"
                              ItemsSource="{Binding Children}">
      <TextBlock Text="{Binding Name}" />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>
相关问题