gpt4 book ai didi

c# - TreeView、HierarchicalDataTemplate 和递归数据

转载 作者:可可西里 更新时间:2023-11-01 07:52:35 25 4
gpt4 key购买 nike

对于我的 TreeView ,我有两个提供 ItemsSource 的不同类。

public class TreeViewModel : ViewModelBase
{
public ObservableCollection<NodeViewModel> Items { get; set; }
}

public class NodeViewModel : ViewModelBase
{
public string Id { get; set; }
public string Name { get; set; }
public ObservableCollection<NodeViewModel> Children { get; set; }
}

现在我希望我的 TreeView 显示 TreeViewModel 中的项目并显示 NodeViewModel 提供的分层数据。

这是我的 XAML

<Window x:Class="TreeViewMasterDetails.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TreeViewMasterDetails"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView Height="Auto"
HorizontalAlignment="Stretch"
Margin="10"
VerticalAlignment="Stretch"
Width="Auto">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="x:Type local:TreeViewModel" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="x:Type local:NodeViewModel" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>

已尝试提供 Items 作为 TreeViewItemsSource。如果显示任何内容,它不会分层显示数据。

我也尝试使用 ItemTemplate 而不是 TreeView.Resources

有什么问题吗?

也许第一个 TextBlock Text Binding 有问题?我想在 Items 中显示 NodeViewModelName 属性。

最佳答案

您只需为 NodeViewModel 声明 HierarchicalDataTemplate,因为这是 TreeView 中唯一显示的内容,并绑定(bind)实际的 ItemSourceTreeView

<TreeView ItemsSource="{Binding Items}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:NodeViewModel}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>

完整示例

Xaml:

<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication13"
Title="MainWindow" x:Name="UI" Width="343" Height="744.625" >

<TreeView DataContext="{Binding ElementName=UI, Path=TreeModel}" ItemsSource="{Binding Items}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:NodeViewModel}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>

</Window>

代码:

public partial class MainWindow : Window
{

public MainWindow()
{
InitializeComponent();
}

public TreeViewModel TreeModel
{
get
{
return new TreeViewModel
{
Items = new ObservableCollection<NodeViewModel>{
new NodeViewModel { Name = "Root", Children = new ObservableCollection<NodeViewModel> {
new NodeViewModel { Name = "Level1" , Children = new ObservableCollection<NodeViewModel>{
new NodeViewModel{ Name = "Level2"}}} } }}
};
}
}
}

public class TreeViewModel
{
public ObservableCollection<NodeViewModel> Items { get; set; }
}

public class NodeViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public ObservableCollection<NodeViewModel> Children { get; set; }
}

结果:

enter image description here

关于c# - TreeView、HierarchicalDataTemplate 和递归数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15240326/

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