绑定来自edmx的自引用表的最佳解决方案是什么,例如:
WPF TreeView控件具有类似以下内容:
解决方法:
我使用此绑定转换器解决了这个问题:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var item = value as MyTable;
return item.MyTable1.Where(i => i.parent_id== item.id); //return children
}
.xaml:
<TreeView Name="treeview1" ItemsSource="{Binding Converter={StaticResource HierarchyConverter}}" ItemTemplate="{StaticResource ItemTemplate}" >
<TreeView.Resources>
<local:HierarchyConverter x:Key="HierarchyConverter" />
<HierarchicalDataTemplate x:Key="ItemTemplate" ItemsSource="{Binding Converter={StaticResource HierarchyConverter}}">
<TextBlock Text="{Binding element_name}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
.cs:
treeview1.ItemsSource = db.MyTable.Where(x => x.partnt_id== null);//elements that have no parent