填充TreeView控件'子节点
本文关键字:子节点 TreeView 控件 填充 | 更新日期: 2023-09-27 18:18:32
我在应用程序中填充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 . example .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帮助我想出这个解决方案。