2

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 named MyTree 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.

Christian
  • 105
  • 1
  • 6
  • Regarding the dynamic table: if you don't want to redraw the whole control (e.g. `DataGrid` with a `DataTable` as `ItemsSource`) I would go with an `ItemsControl` that has a horizontal `ItemsPanel`. In the end adding an item to the `ItemsSource` will result in a new column. So, basically a collection of column items will map to a horizontal `ItemsControl`. Each column item has itself a collection of row items which maps to a vertical `ItemsControl` which is nested into the horizontal `ItemsControl`. A horizontal list of vertical lists. X – BionicCode Nov 03 '19 at 11:48
  • Alternatively create a new `DataTable` each time the grid of the table changes and reassign it to the `DataGrid.ItemsSource` to update the view with the new layout. – BionicCode Nov 03 '19 at 11:50
  • For the `TreeView` you should use `HierarchicalDataTemplate` where each hierarchy shows a (dynamic) table. Then create a data structure of nested tables that reflects your hiearchical table (see my suggestion of column item and row item collections) and populate a `ObservableCollection` with them. Bind this view model collection to the `ItemsSource` of the `TreeView`. Sorry, but I didn't went through your project that you've linked. I don't see a need to introduce a custom `TreeViewItem` at all. – BionicCode Nov 03 '19 at 12:01

0 Answers0