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:
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
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);
}
}
}
Proces class:
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 - 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:
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 code with template:
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):
Proces class: