Skip to content

Commit

Permalink
New API on TreeListView to retrieve parent item (#3355)
Browse files Browse the repository at this point in the history
Fixing binding error on attached property
  • Loading branch information
Keboo authored Oct 31, 2023
1 parent 9980891 commit 44bb9ba
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,58 @@ public void Replace_TopLevelItem_IsReplaced(int indexToReplace)
Assert.Equal(expectedExpanded, treeListViewItemsCollection.GetAllIsExpanded());
}

[Theory]
[InlineData(-1)]
[InlineData(3)]
public void GetParent_WithInvalidIndex_ThrowsOutOfRangeException(int index)
{
//Arrange
ObservableCollection<string> boundCollection = new() { "0", "1", "2" };
TreeListViewItemsCollection<string> treeListViewItemsCollection = new(boundCollection);

//Act/Assert
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => treeListViewItemsCollection.GetParent(index));
Assert.Equal("index", ex.ParamName);
}

[Fact]
public void GetParent_WithNestedItem_ReturnsParent()
{
//Arrange
ObservableCollection<string> boundCollection = new() { "0", "1", "2" };
TreeListViewItemsCollection<string> treeListViewItemsCollection = new(boundCollection);
treeListViewItemsCollection.InsertWithLevel(2, "1_0", 1);
treeListViewItemsCollection.InsertWithLevel(3, "1_1", 1);
treeListViewItemsCollection.InsertWithLevel(4, "1_2", 1);
treeListViewItemsCollection.InsertWithLevel(5, "1_2_0", 2);
treeListViewItemsCollection.InsertWithLevel(6, "1_2_1", 2);
treeListViewItemsCollection.InsertWithLevel(7, "1_2_2", 2);

/*
* 0. 0
* 1. 1
* 2. 1_0
* 3. 1_1
* 4. 1_2
* 5. 1_2_0
* 6. 1_2_1
* 7. 1_2_2
* 8. 2
*/


//Act/Assert
Assert.Null(treeListViewItemsCollection.GetParent(0));
Assert.Null(treeListViewItemsCollection.GetParent(1));
Assert.Equal("1", treeListViewItemsCollection.GetParent(2));
Assert.Equal("1", treeListViewItemsCollection.GetParent(3));
Assert.Equal("1", treeListViewItemsCollection.GetParent(4));
Assert.Equal("1_2", treeListViewItemsCollection.GetParent(5));
Assert.Equal("1_2", treeListViewItemsCollection.GetParent(6));
Assert.Equal("1_2", treeListViewItemsCollection.GetParent(7));
Assert.Null(treeListViewItemsCollection.GetParent(8));
}

private class TestableCollection<T> : ObservableCollection<T>
{
private int _blockCollectionChanges;
Expand Down
15 changes: 15 additions & 0 deletions MaterialDesignThemes.Wpf/Internal/TreeListViewItemsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ public void InsertWithLevel(int index, T item, int level)
}
}

public object? GetParent(int index)
{
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));
int level = ItemLevels[index];
if (level == 0) return null;
for (int i = index - 1; i >= 0; i--)
{
if (ItemLevels[i] == level - 1)
{
return this[i];
}
}
return null;
}

protected override void RemoveItem(int index)
{
int priorNonRootLevelItems = GetPriorNonRootLevelItemsCount(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
</Setter>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="wpf:TreeViewAssist.ExpanderSize" Value="16" />
<Setter Property="wpf:TreeViewAssist.HasNoItemsExpanderVisibility" Value="{Binding RelativeSource={RelativeSource AncestorType=TreeView}, Path=(wpf:TreeViewAssist.HasNoItemsExpanderVisibility)}" />
<Setter Property="wpf:TreeViewAssist.HasNoItemsExpanderVisibility" Value="{Binding RelativeSource={RelativeSource AncestorType=wpf:TreeListView}, Path=(wpf:TreeViewAssist.HasNoItemsExpanderVisibility)}" />
<Setter Property="wpf:TreeViewAssist.ShowSelection" Value="True" />
</Style>

Expand Down
12 changes: 12 additions & 0 deletions MaterialDesignThemes.Wpf/TreeListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ static int GetChildrenAndGrandChildrenCountOfPriorSiblings(TreeListViewItemsColl
}
}

public object? GetParent(object? item)
{
if (InternalItemsSource is { } itemSource &&
ItemContainerGenerator.ContainerFromItem(item) is { } container &&
ItemContainerGenerator.IndexFromContainer(container) is var index &&
index >= 0)
{
return itemSource.GetParent(index);
}
return null;
}

private List<object?> GetExpandedChildrenAndGrandChildren(object? dataItem)
{
List<object?> expandedChildren = new();
Expand Down
6 changes: 0 additions & 6 deletions MaterialDesignThemes.Wpf/TreeListViewItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,6 @@ internal void PrepareTreeListViewItem(object? item, TreeListView treeListView, i
}
}

protected override void OnContentChanged(object oldContent, object newContent)
=> base.OnContentChanged(oldContent, newContent);

protected override void OnContentTemplateChanged(DataTemplate oldContentTemplate, DataTemplate newContentTemplate)
=> base.OnContentTemplateChanged(oldContentTemplate, newContentTemplate);

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Expand Down

0 comments on commit 44bb9ba

Please sign in to comment.