Skip to content

Commit 44bb9ba

Browse files
authored
New API on TreeListView to retrieve parent item (#3355)
Fixing binding error on attached property
1 parent 9980891 commit 44bb9ba

File tree

5 files changed

+80
-7
lines changed

5 files changed

+80
-7
lines changed

MaterialDesignThemes.Wpf.Tests/Internal/TreeListViewItemsCollectionTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,58 @@ public void Replace_TopLevelItem_IsReplaced(int indexToReplace)
591591
Assert.Equal(expectedExpanded, treeListViewItemsCollection.GetAllIsExpanded());
592592
}
593593

594+
[Theory]
595+
[InlineData(-1)]
596+
[InlineData(3)]
597+
public void GetParent_WithInvalidIndex_ThrowsOutOfRangeException(int index)
598+
{
599+
//Arrange
600+
ObservableCollection<string> boundCollection = new() { "0", "1", "2" };
601+
TreeListViewItemsCollection<string> treeListViewItemsCollection = new(boundCollection);
602+
603+
//Act/Assert
604+
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => treeListViewItemsCollection.GetParent(index));
605+
Assert.Equal("index", ex.ParamName);
606+
}
607+
608+
[Fact]
609+
public void GetParent_WithNestedItem_ReturnsParent()
610+
{
611+
//Arrange
612+
ObservableCollection<string> boundCollection = new() { "0", "1", "2" };
613+
TreeListViewItemsCollection<string> treeListViewItemsCollection = new(boundCollection);
614+
treeListViewItemsCollection.InsertWithLevel(2, "1_0", 1);
615+
treeListViewItemsCollection.InsertWithLevel(3, "1_1", 1);
616+
treeListViewItemsCollection.InsertWithLevel(4, "1_2", 1);
617+
treeListViewItemsCollection.InsertWithLevel(5, "1_2_0", 2);
618+
treeListViewItemsCollection.InsertWithLevel(6, "1_2_1", 2);
619+
treeListViewItemsCollection.InsertWithLevel(7, "1_2_2", 2);
620+
621+
/*
622+
* 0. 0
623+
* 1. 1
624+
* 2. 1_0
625+
* 3. 1_1
626+
* 4. 1_2
627+
* 5. 1_2_0
628+
* 6. 1_2_1
629+
* 7. 1_2_2
630+
* 8. 2
631+
*/
632+
633+
634+
//Act/Assert
635+
Assert.Null(treeListViewItemsCollection.GetParent(0));
636+
Assert.Null(treeListViewItemsCollection.GetParent(1));
637+
Assert.Equal("1", treeListViewItemsCollection.GetParent(2));
638+
Assert.Equal("1", treeListViewItemsCollection.GetParent(3));
639+
Assert.Equal("1", treeListViewItemsCollection.GetParent(4));
640+
Assert.Equal("1_2", treeListViewItemsCollection.GetParent(5));
641+
Assert.Equal("1_2", treeListViewItemsCollection.GetParent(6));
642+
Assert.Equal("1_2", treeListViewItemsCollection.GetParent(7));
643+
Assert.Null(treeListViewItemsCollection.GetParent(8));
644+
}
645+
594646
private class TestableCollection<T> : ObservableCollection<T>
595647
{
596648
private int _blockCollectionChanges;

MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ public void InsertWithLevel(int index, T item, int level)
8282
}
8383
}
8484

85+
public object? GetParent(int index)
86+
{
87+
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));
88+
int level = ItemLevels[index];
89+
if (level == 0) return null;
90+
for (int i = index - 1; i >= 0; i--)
91+
{
92+
if (ItemLevels[i] == level - 1)
93+
{
94+
return this[i];
95+
}
96+
}
97+
return null;
98+
}
99+
85100
protected override void RemoveItem(int index)
86101
{
87102
int priorNonRootLevelItems = GetPriorNonRootLevelItemsCount(index);

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TreeListView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
</Setter>
327327
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
328328
<Setter Property="wpf:TreeViewAssist.ExpanderSize" Value="16" />
329-
<Setter Property="wpf:TreeViewAssist.HasNoItemsExpanderVisibility" Value="{Binding RelativeSource={RelativeSource AncestorType=TreeView}, Path=(wpf:TreeViewAssist.HasNoItemsExpanderVisibility)}" />
329+
<Setter Property="wpf:TreeViewAssist.HasNoItemsExpanderVisibility" Value="{Binding RelativeSource={RelativeSource AncestorType=wpf:TreeListView}, Path=(wpf:TreeViewAssist.HasNoItemsExpanderVisibility)}" />
330330
<Setter Property="wpf:TreeViewAssist.ShowSelection" Value="True" />
331331
</Style>
332332

MaterialDesignThemes.Wpf/TreeListView.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ static int GetChildrenAndGrandChildrenCountOfPriorSiblings(TreeListViewItemsColl
202202
}
203203
}
204204

205+
public object? GetParent(object? item)
206+
{
207+
if (InternalItemsSource is { } itemSource &&
208+
ItemContainerGenerator.ContainerFromItem(item) is { } container &&
209+
ItemContainerGenerator.IndexFromContainer(container) is var index &&
210+
index >= 0)
211+
{
212+
return itemSource.GetParent(index);
213+
}
214+
return null;
215+
}
216+
205217
private List<object?> GetExpandedChildrenAndGrandChildren(object? dataItem)
206218
{
207219
List<object?> expandedChildren = new();

MaterialDesignThemes.Wpf/TreeListViewItem.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ internal void PrepareTreeListViewItem(object? item, TreeListView treeListView, i
112112
}
113113
}
114114

115-
protected override void OnContentChanged(object oldContent, object newContent)
116-
=> base.OnContentChanged(oldContent, newContent);
117-
118-
protected override void OnContentTemplateChanged(DataTemplate oldContentTemplate, DataTemplate newContentTemplate)
119-
=> base.OnContentTemplateChanged(oldContentTemplate, newContentTemplate);
120-
121115
public override void OnApplyTemplate()
122116
{
123117
base.OnApplyTemplate();

0 commit comments

Comments
 (0)