Tree View Multiple Level Group Style

1k Views Asked by At

I'm working with a treeview and I want to have a multi-level treeview that groups every level of the tree (All Children).

It seems to be working for the first level of items but when the items have children underneath it the grouping style doesn't seem to be applied.

First Group Showing up but the Items under the First Level arent being grouped.

My Xaml Group Style, HierarchicalDataTemplate, TreeView

        <DataTemplate x:Key="basicGroupStyle">
            <!--Hide the whole grouping header if the group name is empty string-->
            <DockPanel LastChildFill="True" >
                <Border Background="RoyalBlue" 
                        Margin="0" MinHeight="3" Width="5"
                        VerticalAlignment="Center" DockPanel.Dock="Left"/>

                <TextBlock Text="{Binding Name}" 
                            Foreground="Black" 
                            FontSize="8pt" FontWeight="Bold"
                            VerticalAlignment="Center"
                            Margin="5 0 5 0"
                            DockPanel.Dock="Left"/>
                <Border Background="RoyalBlue" 
                        Margin="0" MinHeight="3"
                        VerticalAlignment="Center"/>
            </DockPanel>
        </DataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type explorer:TreeItem}"
                          ItemsSource="{Binding TreeViewItemsView}">
            <!--Hide the whole grouping header if the group name is empty string-->
            <DockPanel LastChildFill="True" >

                <TextBlock Text="{Binding Name}" 
                            Foreground="Black" 
                            FontSize="8pt" FontWeight="Bold"
                            VerticalAlignment="Center"
                            Margin="5 0 5 0"
                            DockPanel.Dock="Left"/>
            </DockPanel>
        </HierarchicalDataTemplate>

    <TreeView ItemsSource="{Binding TreeViewItemsView}">
        <TreeView.GroupStyle>
            <GroupStyle  HeaderTemplate="{StaticResource basicGroupStyle}" />
        </TreeView.GroupStyle>
    </TreeView>

Converters/Group Description

public class AssetTypeExplorerValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var v = value as int?;
        if (v == null)
            return value;

        return Convert(v);
    }

    public static string Convert(int? assetData)
    {
        if (assetData.HasValue)
        {
            if (assetData == 1)
                return "First Group";
            else if (assetData == 2)
                return "Second Group";
            else if (assetData == 3)
                return "Third Group";
            else
                return "Damn";
        }

        return string.Empty;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

TreeItem Class

    public int level { get; set; }

    public string Name { get; set; }

    private ICollectionView _treeViewItemsView;
    private ObservableCollection<TreeItem> _treeViewItems;

    public ObservableCollection<TreeItem> TreeViewItems
    {
        get { return _treeViewItems ?? (TreeViewItems = new ObservableCollection<TreeItem>()); }
        set { _treeViewItems = value; }
    }

    public ICollectionView TreeViewItemsView
    {
        get
        {
            if (_treeViewItemsView == null)
            {
                _treeViewItemsView = CollectionViewSource.GetDefaultView(TreeViewItems) as ListCollectionView;
            }

            return _treeViewItemsView;
        }
    }

    private void Initalize()
    {
        (this.TreeViewItemsView as ListCollectionView).GroupDescriptions.Clear();
        (this.TreeViewItemsView as ListCollectionView).GroupDescriptions.Add(new PropertyGroupDescription("level", new AssetTypeExplorerValueConverter()));

    }

Collections for the TreeItems (Initalize called on constructor)

    private ICollectionView _treeViewItemsView;
    private ObservableCollection<TreeItem> _treeViewItems;
    public ICollectionView TreeViewItemsView
    {
        get
        {
            if (_treeViewItemsView == null)
            {
                _treeViewItemsView = CollectionViewSource.GetDefaultView(TreeViewItems) as ListCollectionView;
            }

            return _treeViewItemsView;
        }

    }

    public ObservableCollection<TreeItem> TreeViewItems
    {
        get { return _treeViewItems ?? (TreeViewItems = new ObservableCollection<TreeItem>()); }
        set { _treeViewItems = value; }
    }

    private void SetupItems()
    {
        var first = new TreeItem() { Name = "First Level First Item", level = 1 };
        var secLevel = new TreeItem() { Name = "Second Level First Item", level = 2 };
        first.TreeViewItems.Add(secLevel);


        TreeViewItems.Add(first);
        TreeViewItems.Add(new TreeItem() { Name = "Second Item", level = 1 });
        TreeViewItems.Add(new TreeItem() { Name = "Third Item", level = 1 });

    }

    private void Initalize()
    {
        (this.TreeViewItemsView as ListCollectionView).GroupDescriptions.Clear();
        (this.TreeViewItemsView as ListCollectionView).GroupDescriptions.Add(new PropertyGroupDescription("level", new AssetTypeExplorerValueConverter()));

        SetupItems();
    }

Any idea why this might be happening? Any help will be greatly appreciated! Thanks!

1

There are 1 best solutions below

0
On

One suggestion;

Assign the HierarchicalDatatemplate to the TreeView.

<HierarchicalDataTemplate x:Key="TestTemplate"  .... HierarchicalDataTemplate>

<TreeView ItemTemplate="{StaticResource TestTemplate}" ..... TreeView>