C# 填充TreeView控件的子节点

C# 填充TreeView控件的子节点,c#,wpf,xaml,treeview,C#,Wpf,Xaml,Treeview,我在应用程序中填充TreeView子节点时遇到问题。我不知道为什么我的对象:objProd不想从objProd.ConsultationTypes.load行加载其实体;load函数给出了以下错误: 错误2“System.Collections.Generic.ICollection” 不包含“加载”的定义,并且没有扩展方法 “Load”接受类型为的第一个参数 'System.Collections.Generic.ICollection' 无法找到您是否缺少using指令或程序集 参考C:\P

我在应用程序中填充TreeView子节点时遇到问题。我不知道为什么我的对象:objProd不想从objProd.ConsultationTypes.load行加载其实体;load函数给出了以下错误:

错误2“System.Collections.Generic.ICollection” 不包含“加载”的定义,并且没有扩展方法 “Load”接受类型为的第一个参数 'System.Collections.Generic.ICollection' 无法找到您是否缺少using指令或程序集 参考C:\Programming\Source\Workspace\360ClientConfigurationPortal\360ClientConfigurationPortal\MainPage.xaml.cs 151 51 360ClientConfigurationPortal

这是我的代码块,它来自对象所在的位置。此代码块是当您单击某个节点以展开它时响应的触发事件

private void ExpandLevel(TreeViewItem parentItem)
            {  
                if (parentItem.Header.Equals("Features"));                      // == typeof(Product)) //Check that the parent items are of type product as we want to fill their inner nodes.
                {
                    Product objProd = parentItem.Header as Product;             //Create instance object of type product
                    if (parentItem.Items.Count > 0)                             //Check if the product parent item has any children
                    {
                        object child = parentItem.Items.GetItemAt(0);           //First Child object set to * during first population
                        if (child.ToString() == "*")                            //If indeed it is a *
                        {                        
                            parentItem.Items.RemoveAt(0);                       //Remove the *

                            objProd.ConsultationTypes.Load();
                            //objProd.Name.ToList();

                            objProd.ConsultationTypes.OrderBy(l => l.Product).ToList().ForEach(l =>
                            {
                                TreeViewItem item = new TreeViewItem();
                                item.Header = l;
                                parentItem.Items.Add(item);

                                l.Consultations.OrderBy(a => a.ConsultationType).ToList().ForEach(a =>
                                {
                                    TreeViewItem attrItem = new TreeViewItem();
                                    attrItem.Header = a;
                                    item.Items.Add(attrItem);
                                });

                                if (l.Consultations.Count > 0)
                                    item.IsExpanded = true;
                            });

                            if (!parentItem.IsExpanded)
                                parentItem.IsExpanded = true;
                            parentItem.IsSelected = true;
                        }
                    }
                }
            }
我通过单击由以下代码创建的根节点来调用上述代码块:

private void PopulateTreeview(SomeEntities ctx)
        {
            TreeViewItem rootItem = new TreeViewItem();
            RootItem root = new RootItem();
            root.Name = "Features";
            rootItem.Header = root.Name;
            productTreeView.Items.Add(rootItem);

            ctx.Products.OrderBy(o => o.Name).ToList().ForEach(d =>
            {
                TreeViewItem item = new TreeViewItem();
                item.Header = d.Name;
                item.Items.Add("*");
                rootItem.Items.Add(item);
            });

            rootItem.IsExpanded = true;
        }
以下函数实际跟踪单击了哪个节点,然后最终调用实际的ExpandLevel方法,该方法用于使用ConsultationTypes填充子节点:

private void tvProducts_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)e.OriginalSource;
            ExpandLevel(item);
        }
以下是相关的xaml:

    <TreeView 
                                            x:Name="productTreeView" 
                                            Margin="18,0,-18,0" 
                                            ItemsSource="{Binding}" 
                                            Height="300"
                                            TreeViewItem.Expanded="tvProducts_Expanded"
                                            >
                                        </TreeView>
 <DataTemplate DataType="{x:Type local:ConsultationType}">
            <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                <Image x:Name="Icon" Height="16" Width="16" Source="/Images/VSClass16.png" />
                <TextBlock Margin="2,0,0,0" VerticalAlignment="Center" Text="{Binding ProductId}" />
                <TextBlock VerticalAlignment="Center" Text="." />
                <TextBlock x:Name="Name" Margin="4,0,0,0" VerticalAlignment="Center" Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate

您需要使用显式加载:

var objectContext = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext;
objectContext.LoadProperty(objProd, o => o.ConsultationTypes);
而不是

objProd.ConsultationTypes.Load();

在方法ExpandLevel中,我最终是这样做的:

private void GetTreeViewData()
        {
            List<Heirachi> result = new List<Heirachi>();
            //get all heirachi
            using (var c = new IWHSMacsteelEntities())
            {
               result  = (from p in c.Products
                             from ct in c.ConsultationTypes
                             where p.Id == ct.ProductId
                             orderby p.Id
                             select new Heirachi
                             {
                                 Name = p.Name,
                                 Code = ct.Code,
                                 ConsTypeDesc = ct.Name
                             }).ToList();



            }
            //distinct product
            var distinctResult = result.Select(s=>s.Name).Distinct();          

            //populate parent treeview
            TreeViewItem rootItem = new TreeViewItem();
            RootItem root = new RootItem();

            root.Name = "Features";
            rootItem.Header = root;
            productTreeView.Items.Add(rootItem);


            foreach (var dr in distinctResult)
            {
                TreeViewItem item = new TreeViewItem();
                item.Header = dr.ToString();
                //item.Items.Add("*");
                rootItem.Items.Add(item);

               //add children
                result.Where(s => s.Name == dr.ToString()).Select(s => s.ConsTypeDesc).ToList().ForEach(i =>
                    {
                        TreeViewItem childItem = new TreeViewItem();
                        childItem.Header = i.ToString();
                        //item.Items.Add("*");
                        item.Items.Add(childItem);

                    });

            }
            rootItem.IsExpanded = true;
        }

非常感谢Bongolwethu帮助我提出此解决方案。

我从您的回答中得到了一个由于LoadProperty引起的错误:错误2''U 360ClientConfigurationPortal.IWHSMacsteelEntities'不包含'LoadProperty'的定义,并且没有扩展方法'LoadProperty'接受类型的第一个参数找不到“ConfigurationPortal.SomeEntities”是否缺少using指令或程序集引用?C:\Programming\Source\Workspace\ConfigurationPortal\ConfigurationPortal\MainPage.xaml.cs 154 34ConfigurationPortal@Sizons尝试使用var objectContext=ctx作为System.Data.Entity.Infrastructure.IObjectContextAdapter.objectContext;然后是objectContext.LoadPropertyobjProd,o=>o.ConsultationTypes;我也这样尝试过,但在LoadProperty上又出现了一些错误。