Skip to content

Commit

Permalink
Adding template selector support
Browse files Browse the repository at this point in the history
  • Loading branch information
Keboo committed Oct 24, 2023
1 parent 1fdce0e commit bc52078
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<UserControl x:Class="MaterialDesignThemes.UITests.WPF.TreeListViews.TreeListViewTemplateSelector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MaterialDesignThemes.UITests.WPF.TreeListViews"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
d:DesignHeight="450" d:DesignWidth="800">
<materialDesign:TreeListView
x:Name="TreeListView"
ItemsSource="{Binding Items}">
<materialDesign:TreeListView.ItemTemplateSelector>
<local:TypeTemplateSelector>
<local:TypeTemplateSelector.NumberTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:TreeItem}"
ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Value}" Foreground="Red" />
</HierarchicalDataTemplate>
</local:TypeTemplateSelector.NumberTemplate>

<local:TypeTemplateSelector.StringTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:TreeItem}"
ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Value}" Foreground="Blue" />
</HierarchicalDataTemplate>
</local:TypeTemplateSelector.StringTemplate>
</local:TypeTemplateSelector>
</materialDesign:TreeListView.ItemTemplateSelector>
</materialDesign:TreeListView>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.ObjectModel;

namespace MaterialDesignThemes.UITests.WPF.TreeListViews;

/// <summary>
/// Interaction logic for TreeListViewTemplateSelector.xaml
/// </summary>
public partial class TreeListViewTemplateSelector
{
public ObservableCollection<TreeItem> Items { get; } = new();


public TreeListViewTemplateSelector()
{
InitializeComponent();

AddChildren("Foo");
AddChildren("42");
AddChildren("24", "a", "b", "c");
AddChildren("Bar", "1", "2", "3");

void AddChildren(string root, params string[] children)
{
TreeItem item = new(root, null);
foreach(string child in children)
{
item.Children.Add(new TreeItem(child, item));
}
Items.Add(item);
}
}
}

public class TypeTemplateSelector : DataTemplateSelector
{
public DataTemplate? NumberTemplate { get; set; }
public DataTemplate? StringTemplate { get; set; }

public override DataTemplate? SelectTemplate(object? item, DependencyObject container)
{
if (item is TreeItem treeItem)
{
if (int.TryParse(treeItem.Value, out _))
{
return NumberTemplate;
}
return StringTemplate;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Windows.Media;

namespace MaterialDesignThemes.UITests.WPF.TreeListViews;

public class TreeListViewTests : TestBase
{
public TreeListViewTests(ITestOutputHelper output)
: base(output)
{
AttachedDebuggerToRemoteProcess = true;
AttachedDebuggerToRemoteProcess = false;
}

[Fact]
Expand Down Expand Up @@ -930,6 +932,35 @@ public async Task TopLevelItemWhichHasBeenExpandedAndCollapsed_MovesAndMaintains
recorder.Success();
}

[Fact]
public async Task TreeListView_WithTemplateSelector_UsesSelectorTemplates()
{
await using var recorder = new TestRecorder(App);

IVisualElement<TreeListView> treeListView = (await LoadUserControl<TreeListViewTemplateSelector>()).As<TreeListView>();

IVisualElement<TreeListViewItem> item3 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[2]");
IVisualElement<TreeListViewItem> item4 = await treeListView.GetElement<TreeListViewItem>("/TreeListViewItem[3]");

await item3.LeftClickExpander();
await Task.Delay(500);
await item4.LeftClickExpander();
await Task.Delay(500);

await AssertTreeItemContent(treeListView, 0, "Foo", Colors.Blue);
await AssertTreeItemContent(treeListView, 1, "42", Colors.Red);
await AssertTreeItemContent(treeListView, 2, "24", Colors.Red, true);
await AssertTreeItemContent(treeListView, 3, "a", Colors.Blue);
await AssertTreeItemContent(treeListView, 4, "b", Colors.Blue);
await AssertTreeItemContent(treeListView, 5, "c", Colors.Blue);
await AssertTreeItemContent(treeListView, 6, "Bar", Colors.Blue, true);
await AssertTreeItemContent(treeListView, 7, "1", Colors.Red);
await AssertTreeItemContent(treeListView, 8, "2", Colors.Red);
await AssertTreeItemContent(treeListView, 9, "3", Colors.Red);

recorder.Success();
}

private static async Task AssertTreeItemContent(IVisualElement<TreeListView> treeListView, int index, string content, bool isExpanded = false)
{
await Wait.For(async () =>
Expand All @@ -940,6 +971,23 @@ await Wait.For(async () =>
});
}

private static async Task AssertTreeItemContent(
IVisualElement<TreeListView> treeListView,
int index,
string content,
Color foreground,
bool isExpanded = false)
{
await Wait.For(async () =>
{
IVisualElement<TreeListViewItem> treeItem = await treeListView.GetElement<TreeListViewItem>($"/TreeListViewItem[{index}]");
Assert.Equal(content, await treeItem.GetContentText());
Assert.Equal(isExpanded, await treeItem.GetIsExpanded());
IVisualElement<TextBlock> textBlock = await treeItem.GetElement<TextBlock>();
Assert.Equal(foreground, await textBlock.GetForegroundColor());
});
}

private static async Task AddChildren(IVisualElement<TreeListViewItem> item, int numChildren, IVisualElement<Button> addButton)
{
await item.LeftClick();
Expand Down
8 changes: 6 additions & 2 deletions MaterialDesignThemes.Wpf/TreeListViewItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ private void OnChildrenChanged(NotifyCollectionChangedEventArgs e)

internal void PrepareTreeListViewItem(object? item, TreeListView treeListView, int level, bool isExpanded)
{
//TODO: Handle template selector
if (ContentTemplate is HierarchicalDataTemplate { ItemsSource: { } itemsSourceBinding })
if (GetTemplate() is HierarchicalDataTemplate { ItemsSource: { } itemsSourceBinding })
{
SetBinding(ChildrenProperty, itemsSourceBinding);
}
IsExpanded = isExpanded;
Level = level;
TreeListView = treeListView;

DataTemplate GetTemplate()
{
return ContentTemplate ?? ContentTemplateSelector.SelectTemplate(item, this);
}
}

internal void ClearTreeListViewItem(object item, TreeListView treeListView)
Expand Down

0 comments on commit bc52078

Please sign in to comment.