【问题标题】:Populating the TreeView controls' child nodes填充 TreeView 控件的子节点
【发布时间】:2015-10-21 15:31:52
【问题描述】:

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

错误 2 'System.Collections.Generic.ICollection<_configurationportal.consultationtype>' 不包含“加载”的定义,也没有扩展方法 'Load' 接受类型的第一个参数 'System.Collections.Generic.ICollection<_360clientconfigurationportal.consultationtype>' 可以找到(您是否缺少 using 指令或程序集 参考?)C:\Programming\Source\Workspaces\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

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    你需要使用显式加载:

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

    而不是

    objProd.ConsultationTypes.Load();
    

    在“ExpandLevel”方法中

    【讨论】:

    • 由于您的回答中的 LoadProperty,我收到一个错误:错误 2 '_360ClientConfigurationPortal.IWHSMacsteelEntities' 不包含 'LoadProperty' 的定义,并且没有扩展方法 'LoadProperty' 接受类型的第一个参数可以找到“ConfigurationPortal.SomeEntities”(您是否缺少 using 指令或程序集引用?) C:\Programming\Source\Workspaces\ConfigurationPortal\ConfigurationPortal\MainPage.xaml.cs 154 34 ConfigurationPortal
    • @Sizons 尝试var objectContext = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext; 然后objectContext.LoadProperty(objProd, o =&gt; o.ConsultationTypes);
    • 我也尝试过这种方式,但 LoadProperty 再次出现错误。
    【解决方案2】:

    我最终这样做了:

    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 帮助我提出这个解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2015-11-14
      • 2013-07-16
      • 1970-01-01
      相关资源
      最近更新 更多