【发布时间】: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
【问题讨论】: