From bf8e24b0c7f1270c88675c70ec14f3884870669b Mon Sep 17 00:00:00 2001 From: Kevin B Date: Thu, 2 Nov 2023 14:44:16 -0700 Subject: [PATCH] Allow ICollectionView for nested TreeListViewItems (#3358) This requires supported non-generic IEnumerable --- .../WPF/TreeListViews/TreeItem.cs | 7 ++++++- .../TreeListViewWithCollectionView.xaml | 2 +- MaterialDesignThemes.Wpf/TreeListViewItem.cs | 13 +++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/MaterialDesignThemes.UITests/WPF/TreeListViews/TreeItem.cs b/MaterialDesignThemes.UITests/WPF/TreeListViews/TreeItem.cs index 9bdcdd6c5a..dc92ac8f65 100644 --- a/MaterialDesignThemes.UITests/WPF/TreeListViews/TreeItem.cs +++ b/MaterialDesignThemes.UITests/WPF/TreeListViews/TreeItem.cs @@ -1,4 +1,6 @@ -using System.Diagnostics; +using System.ComponentModel; +using System.Diagnostics; +using System.Windows.Data; namespace MaterialDesignThemes.UITests.WPF.TreeListViews; @@ -12,9 +14,12 @@ public class TreeItem //NB: making the assumption changes occur ont he UI thread public TestableCollection Children { get; } = new(); + public ICollectionView ChildrenView { get; } + public TreeItem(string value, TreeItem? parent) { Value = value; Parent = parent; + ChildrenView = CollectionViewSource.GetDefaultView(Children); } } diff --git a/MaterialDesignThemes.UITests/WPF/TreeListViews/TreeListViewWithCollectionView.xaml b/MaterialDesignThemes.UITests/WPF/TreeListViews/TreeListViewWithCollectionView.xaml index 5c5cadad51..3054b944b1 100644 --- a/MaterialDesignThemes.UITests/WPF/TreeListViews/TreeListViewWithCollectionView.xaml +++ b/MaterialDesignThemes.UITests/WPF/TreeListViews/TreeListViewWithCollectionView.xaml @@ -11,7 +11,7 @@ mc:Ignorable="d"> - + diff --git a/MaterialDesignThemes.Wpf/TreeListViewItem.cs b/MaterialDesignThemes.Wpf/TreeListViewItem.cs index e6bc294920..cd53cec5be 100644 --- a/MaterialDesignThemes.Wpf/TreeListViewItem.cs +++ b/MaterialDesignThemes.Wpf/TreeListViewItem.cs @@ -1,4 +1,5 @@ -using System.Collections.Specialized; +using System.Collections; +using System.Collections.Specialized; using MaterialDesignThemes.Wpf.Internal; namespace MaterialDesignThemes.Wpf; @@ -16,7 +17,7 @@ public TreeListViewItem() private TreeListViewContentPresenter? ContentPresenter { get; set; } private TreeListView? TreeListView { get; set; } - public IEnumerable GetChildren() => Children ?? Array.Empty(); + public IEnumerable GetChildren() => Children?.OfType() ?? Array.Empty(); public bool IsExpanded { @@ -55,14 +56,14 @@ public int Level DependencyProperty.Register(nameof(Level), typeof(int), typeof(TreeListViewItem), new PropertyMetadata(0)); - internal IEnumerable? Children + internal IEnumerable? Children { - get => (IEnumerable)GetValue(ChildrenProperty); + get => (IEnumerable?)GetValue(ChildrenProperty); set => SetValue(ChildrenProperty, value); } internal static readonly DependencyProperty ChildrenProperty = - DependencyProperty.Register("Children", typeof(IEnumerable), typeof(TreeListViewItem), + DependencyProperty.Register(nameof(Children), typeof(IEnumerable), typeof(TreeListViewItem), new PropertyMetadata(null, OnChildrenChanged)); private static void OnChildrenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) @@ -140,7 +141,7 @@ internal void ClearTreeListViewItem(object item, TreeListView treeListView) private void UpdateHasChildren() { - SetCurrentValue(HasItemsProperty, Children?.Any() == true); + SetCurrentValue(HasItemsProperty, GetChildren().Any()); } protected override void OnMouseDoubleClick(MouseButtonEventArgs e)