具有多列的Treeview

编程入门 行业动态 更新时间:2024-10-28 16:20:18
本文介绍了具有多列的Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在WPF中使用多列创建树视图。我很清楚,关于这个主题确实有很多问题。但是,在绑定数据时,它们似乎采取了不同的方法。每个人似乎都设置了itemsource,就像我在后面的代码中填充treeview.items的位置一样。这也是我不确定是否要使用ItemTemplate / HierarchicalDataTemplate或完成它的正确方法的原因。 (我觉得这应该是一个简单的步骤。)

I am trying to create a treeview in WPF with multiple columns. I am well aware, that there are really numerous questions regarding this subject. However they seem to take a different approach when binding the data. Everybody seems to set the itemssource, as where I fill the treeview.items in de code behind. That is also the reason I am not sure whether to use ItemTemplate / HierarchicalDataTemplate or the correct way to accomplish it. (I have the feeling that this should be an easy step.)

我现在拥有的代码如下:

The code I have now is as follows:

Mainwindow.xaml中的树视图

Treeview in Mainwindow.xaml

<TreeView x:Name="ProcesTree" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <TreeView.ItemTemplate> <ItemContainerTemplate> <Grid> <TextBlock Text="{Binding procesNummer}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0"/> <TextBlock Text="{Binding procesNaam}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0"/> </Grid> </ItemContainerTemplate> </TreeView.ItemTemplate> </TreeView>

Mainwindow.xaml.cs

Mainwindow.xaml.cs

public List<Proces> processen = new List<Proces>(); public MainWindow() { InitializeComponent(); processen = Database.getHoofdProcessen(); processen = Extensions.OrderByAlphaNumeric(processen, p => p.procesNummer); foreach (Proces p in processen) { writeProcesses(p, ProcesTree.Items); } } public void writeProcesses(Proces p, ItemCollection tv) { tv.Add(p); List<Proces> processen = Database.getProcessenOnNummer((p.procesNummer + ".%")); if (processen.Count != 0) { foreach (Proces pr in processen) { writeProcesses(pr, p.Items); } } }

过程类别:

public class Proces : TreeViewItem { public Int64 procesId { get; set; } public String procesNummer { get; set; } public String procesNaam { get; set; } public String procesOmschrijvig { get; set; } public Boolean procesEinde { get; set; } }

@Edit-要清楚,我现在具有如下结构图片。唯一遗漏的是模板?如下所示:

@Edit - To be clear, I now have a structure like the following image. The only thing that misses is the template? as how it should be shown:

推荐答案

我偶然发现了一种简单的方法来完成自己的尝试。无需使用第三方控件或非常复杂的模板。感谢以下链接: www.codemag/Article/1401031

I have stumbled upon an easy way to accomplish what I was trying. Without using third-party controls or very complex templates. Thanks to the following link: www.codemag/Article/1401031

由于我很难在此找到任何有用的东西,因此我决定自己发布一个答案,以便其他人也可以使用它。

Since I had a really hard time finding anything usefull on this, I decided to post an answer myself, so others can also make use of it.

带有模板的树形视图代码:

<TreeView BorderThickness="0" ItemsSource="{Binding processes}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding subProcesses}" > <Grid> //Number of columns <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> //Controls to use in the treeview grid <TextBlock Text="{Binding procesId}" Grid.Column="0"/> <TextBlock Text="{Binding procesName}" Grid.Column="1"/> </Grid> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>

要绑定到树视图的列表(每次您将向Proces列出它将生成的子流程的列表

List to bind to the treeview (Every time you will give the Proces a list of subProcesses it will generate an additional level in the treeview):

List<Proces> processes;

过程类别:

public class Proces { public Int64 procesId { get; set; } public String procesName { get; set; } public List<Proces> subProcesses { get; set; } }

更多推荐

具有多列的Treeview

本文发布于:2023-06-13 07:16:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/676613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有多   Treeview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!