gpt4 book ai didi

c# - 如何使用 TreeView 的转换器从列表创建树结构?

转载 作者:太空宇宙 更新时间:2023-11-03 10:37:22 25 4
gpt4 key购买 nike

一些函数提供了一个简单的字符串列表,如下所示:

var list = new List<int>{1,2,3,4};

现在 TreeView 想要将此列表转换为具有两个根节点(一个用于奇数,一个用于偶数)的树。这只是一个例子,因为真实的场景必须创建一个层次结构更多的结构。关键是后端提供了一个平面列表, View 想把它转换成树。

我们尝试将 Converter 作为 ItemsSource,但是当它创建一个新结构时,它基本上打破了与原始列表的绑定(bind)(使得异步填充变得不可能)。 Explaining why

这是一个小的复制代码:

代码隐藏:

public partial class MainWindow : Window
{
public ObservableCollection<int> TreeViewSource { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
TreeViewSource = new ObservableCollection<int>();

Action filler =() => { Enumerable.Range(0, 100).ToList().ForEach((i) => { Thread.Sleep(20); TreeViewSource.Add(i); }); };

Task.Run(() => filler()); // ASYNC CALL DOES NOT WORK
//filler(); // SYNC CALL DOES WORK
}
}
public class TreeConverter : IValueConverter
{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var sourceCollection = value as ObservableCollection<int>;
var outputCollection = new List<Item>();

var odd = new Item { Text = "not divisible by 2" };
var even = new Item { Text = "divisible by 2" };
even.Children.AddRange(sourceCollection.Where(x => x % 2 == 0).Select(x => new Item { Text = x.ToString() }));
odd.Children.AddRange(sourceCollection.Where(x => x % 2 != 0).Select(x => new Item { Text = x.ToString() }));

outputCollection.Add(odd);
outputCollection.Add(even);
return outputCollection;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class Item
{
public string Text { get; set; }
public List<Item> Children { get; set; }
public Item()
{
Children = new List<Item>();
}
}

Xaml:

<Window x:Class="TreeViewAsync.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TreeViewAsync"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:TreeConverter x:Key="treeConverter"/>
</Window.Resources>
<Grid>
<TreeView Name="treeView"
ItemsSource="{Binding TreeViewSource, Converter={StaticResource treeConverter}}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Item}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Text}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>

link表示应该在 ItemsSource 上使用 DataTemplate 而不是 Converter。但是,由于模板是单独应用于每个项目的,它如何创建一个完整的树?

同样:ViewModel 不需要数据是树,因此不提供树结构,而只提供平面列表。为了方便起见, View 希望将其显示为树。由于 MVVM,我们希望避免 View 中的代码隐藏。

最佳答案

A TreeView now wants to transform this list into a tree with two root nodes The ViewModel does not need the data to be a tree and therefore does not provide a treestructure but a flat list only

您误解了 View 模型的概念。如果 View 需要分层数据,那么 View 模型的职责是使数据分层并准备好与 View 绑定(bind)。

But since the template is applied to each item individually

通常,模板适用于任何类型的项目。

public sealed class NestedItemsViewModel
{
public string Name { get; set; }
public int[] Items { get; set; }
}

public sealed class ViewModel
{
private readonly List<int> list = new List<int> { 1, 2, 3, 4 };
private NestedItemsViewModel[] items;

public NestedItemsViewModel[] Items
{
get
{
if (items == null)
{
items = new[]
{
new NestedItemsViewModel
{
Name = "Even",
Items = list.Where(x => x % 2 == 0).ToArray()
},
new NestedItemsViewModel
{
Name = "Odd",
Items = list.Where(x => x % 2 != 0).ToArray()
},
};
}
return items;
}
}
}

XAML:

<!-- Assuming, that TreeView.DataContext = new ViewModel()  -->
<TreeView ItemsSource="{Binding Items, IsAsync=True}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:NestedItemsViewModel}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<!-- If nested items will be more complex, then here will be their data template(s) -->
</TreeView.Resources>
</TreeView>

关于c# - 如何使用 TreeView 的转换器从列表创建树结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27184691/

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