gpt4 book ai didi

c# - 如何将自引用表绑定(bind)到 WPF TreeView

转载 作者:行者123 更新时间:2023-11-30 15:36:02 31 4
gpt4 key购买 nike

从 edmx 绑定(bind)自引用表的最佳解决方案是什么:

enter image description here

WPF TreeView 控件有类似的东西:

enter image description here

最佳答案

我使用这个 Binding Converter 解决了这个问题:

    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

关于c# - 如何将自引用表绑定(bind)到 WPF TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161963/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com