Skip to content

Commit

Permalink
Allow ICollectionView for nested TreeListViewItems (#3358)
Browse files Browse the repository at this point in the history
This requires supported non-generic IEnumerable
  • Loading branch information
Keboo authored Nov 2, 2023
1 parent dc37697 commit bf8e24b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
7 changes: 6 additions & 1 deletion MaterialDesignThemes.UITests/WPF/TreeListViews/TreeItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Diagnostics;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Data;

namespace MaterialDesignThemes.UITests.WPF.TreeListViews;

Expand All @@ -12,9 +14,12 @@ public class TreeItem
//NB: making the assumption changes occur ont he UI thread
public TestableCollection<TreeItem> Children { get; } = new();

public ICollectionView ChildrenView { get; }

public TreeItem(string value, TreeItem? parent)
{
Value = value;
Parent = parent;
ChildrenView = CollectionViewSource.GetDefaultView(Children);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
mc:Ignorable="d">
<materialDesign:TreeListView ItemsSource="{Binding Items}">
<materialDesign:TreeListView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:TreeItem}" ItemsSource="{Binding Children}">
<HierarchicalDataTemplate DataType="{x:Type local:TreeItem}" ItemsSource="{Binding ChildrenView}">
<TextBlock Text="{Binding Value}" />
</HierarchicalDataTemplate>
</materialDesign:TreeListView.Resources>
Expand Down
13 changes: 7 additions & 6 deletions MaterialDesignThemes.Wpf/TreeListViewItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Specialized;
using System.Collections;
using System.Collections.Specialized;
using MaterialDesignThemes.Wpf.Internal;

namespace MaterialDesignThemes.Wpf;
Expand All @@ -16,7 +17,7 @@ public TreeListViewItem()
private TreeListViewContentPresenter? ContentPresenter { get; set; }
private TreeListView? TreeListView { get; set; }

public IEnumerable<object?> GetChildren() => Children ?? Array.Empty<object?>();
public IEnumerable<object?> GetChildren() => Children?.OfType<object?>() ?? Array.Empty<object?>();

public bool IsExpanded
{
Expand Down Expand Up @@ -55,14 +56,14 @@ public int Level
DependencyProperty.Register(nameof(Level), typeof(int), typeof(TreeListViewItem), new PropertyMetadata(0));


internal IEnumerable<object?>? Children
internal IEnumerable? Children
{
get => (IEnumerable<object?>)GetValue(ChildrenProperty);
get => (IEnumerable?)GetValue(ChildrenProperty);
set => SetValue(ChildrenProperty, value);
}

internal static readonly DependencyProperty ChildrenProperty =
DependencyProperty.Register("Children", typeof(IEnumerable<object?>), typeof(TreeListViewItem),
DependencyProperty.Register(nameof(Children), typeof(IEnumerable), typeof(TreeListViewItem),
new PropertyMetadata(null, OnChildrenChanged));

private static void OnChildrenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bf8e24b

Please sign in to comment.