HierarchicalDataTemplate和许多一对多与有效载荷实体导航绑定问题 [英] HierarchicalDataTemplate and Many-To-Many with Payload Entities Navigation Binding Issue

查看:191
本文介绍了HierarchicalDataTemplate和许多一对多与有效载荷实体导航绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个多到很多与有效载荷的实体,如下图所示:

I have two Many-To-Many with Payload entities as shown below:

因此​​,为了使一个大会存储在MasterPartNumber和一个部件号: MasterPartNumber.pn ,我用的导航属性 ParentBOMs ,这是由关系式:
MasterPartNumber.pnID = MasterPartsList.parentPnID
这给了我父装配下的所有子pnIDs。

So, to make an Assembly stored in MasterPartNumber with a part number: MasterPartNumber.pn, I use the navigation property ParentBOMs, which is given by the relationship: MasterPartNumber.pnID = MasterPartsList.parentPnID. This gives me all the child pnIDs under that parent assembly.

要获得儿童零件号为组装,我用的是 ChildPn 导航属性,由 MasterPartsList定义。 PNID = MasterPartNumber.pnID

To get the child part numbers for that assembly, I use the ChildPn navigation property, defined by MasterPartsList.pnID = MasterPartNumber.pnID.

请注意,顶层装配项目不在上市MasterPartsList(他们将有一个parentPnID为null)。

Please note that top level assembly items are NOT listed in MasterPartsList (they would have a parentPnID that is null).

我的TreeView HierarchicalDataTemplate绑定是:

My TreeView HierarchicalDataTemplate binding is:

<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)为有孩子的信息的每个项目。

Which I believe to be correct. I can step through the debugger and see that the entire BOMItem navigation properties are populated (ParentBOM.ChildPn.pn) for each item that has child information.

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

WHY am I unable to see these child properties populated in my TreeView?!

What I should get:

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

我实际上得到:

Root Assembly

我是否需要额外的转换器?我需要定义我的ObservableCollection对象包装的吸气,进一步?

Do I need an additional converter? Do I need to define my ObservableCollection object wrapper's "getter" further?

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`. 

请帮帮忙!谢谢!

推荐答案

不能够得到预期的输出应该是什么想法?你可以绘制树和张贴。

Not able to get an idea of what the expected output should be ? Can you draw the tree and post it.

您可能必须创建一个包装VM类型要present域类型。说.. MasterPartVM。现在,您可以定义逻辑属性为2路由例如儿童。您hierararchical数据模板将一直扩展使用该属性的一个新的水平。

You may have to create a wrapper VM type for the domain type you want to present. say.. MasterPartVM. You can now define a property with the logic for the 2 routes e.g. Children. Your hierararchical data template will always expand the next level using this property.

这将使找到您的实现MasterPartVM.Children权儿童的责任 - 你似乎已经板上钉钉了。让我知道如果我误解了问题。

This will put the onus of finding the right children on your implementation of MasterPartVM.Children - which you seem to have nailed down. Let me know if I have misunderstood the problem..

例如。在链接后,你可以看到,我用的包装特性结合群和条目整理一个列表。

e.g. In the linked post, you can see that I used a wrapper property to combine Subgroups and Entries to collate a single list.

下面是另外一个例子。

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>

这篇关于HierarchicalDataTemplate和许多一对多与有效载荷实体导航绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆