gpt4 book ai didi

c# - 如何在 XAML 中具有递归或层次结构列表和子列表?

转载 作者:行者123 更新时间:2023-11-30 18:32:48 25 4
gpt4 key购买 nike

假设我有以下类(class),Employee

public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public ObservableCollection<Employee> Underlings { get; set; }
}

然后我有以下 XAML,绑定(bind)到 ObservableCollection<Employee> MyEmployees

<ListBox ItemsSource="{Binding Path=MyEmployees}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Tag="{Binding Path=Employee.Id}">
<TextBlock Text="{Binding Path=Employee.Name}"></TextBlock>

<!-- Here's where I declare my underlings -->
<ListBox ItemsSource="{Binding Path=Employee.Underlings}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Tag="{Binding Path=Employee.Id}">
<TextBlock Text="{Binding Path=Employee.Name}"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

这允许集合中的每个员工 MyEmployees有一些下属。不过那些手下也是员工类型,可以有自己的手下。如何在不使我的 XAML 变得非常复杂的情况下满足这些额外级别的要求?

有什么方法可以声明我的 DataTemplate单独并允许它在自身内部被引用?

我是否必须改为从代码隐藏中执行所有这些操作?

(我意识到上面的 XAML 可能不是 100% 正确,这只是一个例子)

最佳答案

因此您必须使用 TreeView 而不是 ListBox。并且您必须指定一个 HierarchicalDataTemplate

关于c# - 如何在 XAML 中具有递归或层次结构列表和子列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18310589/

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