I am working on a WPF application which uses a hierarchical table. I made a sample project that works well, but there is another specification which must be provided.
I used the following Microsoft sample to make the project: https://github.com/ariesy/WPFSamples/tree/master/TreeListView/cs
In the sample I have 3 columns now but I have to make it dynamically changeable at runtime. It means that I need to remove some columns or add new ones on user events. How can I solve this?
I tried some stuff but I am still not so experienced in WPF and unfortunately I had to give up because it took to much time for me, so I need a little help - some useful references, ideas etc.
I uploaded my sample project to github because it is too large to share the whole code here (treeListView branch): https://github.com/mystic997/UnityIocSample/tree/sample/treeListView
The following files in this project are important to my question:
- UnityIoCWpfSample.UI.Views.TreeListViewTable.TreeListView.xaml
- UnityIoCWpfSample.UI.Views.TreeListViewTable.TreeListViewModel - this
ViewModel
class has a property namedMyTree
which is binded to a ContentControl in the previous xaml - UnityIoCWpfSample.UI.Views.TreeListViewTable.CustomControls.TreeListView
- UnityIoCWpfSample.UI.Views.TreeListViewTable.CustomControls.TreeListViewItem
I think I need to refactor my GridViewColumnCollection
which has a key named gvcc
.
<GridViewColumnCollection x:Key="gvcc">
<GridViewColumn Header="Title"
CellTemplate="{StaticResource HierarchicColumn}"
Width="250" />
<GridViewColumn Header="Description"
DisplayMemberBinding="{Binding Text}"
Width="200" />
<GridViewColumn Header="IsEnabled"
Width="60">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center"
IsChecked="{Binding IsEnabled}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridViewColumnCollection>
And need to store the columns in my TreeListViewItem
class:
public class TreeListViewItem : TreeViewItem
{
public object Content { get; set; }
private int _level = -1;
public int Level
{
get
{
if (_level == -1)
{
TreeListViewItem parent = ItemsControlFromItemContainer(this) as TreeListViewItem;
_level = (parent != null) ? parent.Level + 1 : 0;
}
return _level;
}
}
protected override DependencyObject GetContainerForItemOverride()
{
return new TreeListViewItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is TreeListViewItem;
}
}
But if I do this, my first column which has a hierarchical template, can't be implemented easily.