具有多列的Treeview [英] Treeview with multiple columns
问题描述
我正在尝试在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:
推荐答案
我偶然发现了一种简单的方法来完成自己的尝试。无需使用第三方控件或非常复杂的模板。感谢以下链接:
http://www.codemag.com/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: http://www.codemag.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!