Skip to content

Commit d39f427

Browse files
authored
Fixing issue with the template selector not getting forwarded. (#3405)
1 parent c24ebd0 commit d39f427

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<UserControl x:Class="MaterialDesignThemes.UITests.Samples.PopupBox.PopupBoxWithTemplateSelector"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="clr-namespace:MaterialDesignThemes.UITests.Samples.PopupBox" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
7+
mc:Ignorable="d"
8+
d:DesignHeight="450" d:DesignWidth="800">
9+
<UserControl.Resources>
10+
<DataTemplate x:Key="OddTemplate">
11+
<Border Background="Red" Width="10" Height="10" />
12+
</DataTemplate>
13+
14+
<DataTemplate x:Key="EvenTemplate">
15+
<Border Background="Blue" Width="10" Height="10" />
16+
</DataTemplate>
17+
18+
<local:ColorTemplateSelector x:Key="ColorTemplateSelector"
19+
OddTemplate="{StaticResource OddTemplate}"
20+
EvenTemplate="{StaticResource EvenTemplate}" />
21+
</UserControl.Resources>
22+
<StackPanel>
23+
<materialDesign:PopupBox
24+
25+
ToggleContentTemplateSelector="{StaticResource ColorTemplateSelector}" x:Name="MyPopupBox"/>
26+
<Button Content="Toggle" Click="Button_Click" />
27+
</StackPanel>
28+
</UserControl>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+

2+
namespace MaterialDesignThemes.UITests.Samples.PopupBox;
3+
4+
/// <summary>
5+
/// Interaction logic for PopupBoxWithTemplateSelector.xaml
6+
/// </summary>
7+
public partial class PopupBoxWithTemplateSelector : UserControl
8+
{
9+
private int _counter;
10+
public PopupBoxWithTemplateSelector()
11+
{
12+
InitializeComponent();
13+
MyPopupBox.ToggleContent = _counter;
14+
}
15+
16+
private void Button_Click(object sender, RoutedEventArgs e)
17+
{
18+
MyPopupBox.ToggleContent = ++_counter;
19+
}
20+
}
21+
22+
public class ColorTemplateSelector : DataTemplateSelector
23+
{
24+
public DataTemplate? OddTemplate { get; set; }
25+
public DataTemplate? EvenTemplate { get; set; }
26+
27+
public override DataTemplate? SelectTemplate(object? item, DependencyObject container)
28+
{
29+
if (item is int intValue)
30+
{
31+
return intValue % 2 == 0 ? EvenTemplate : OddTemplate;
32+
}
33+
return null;
34+
}
35+
}

MaterialDesignThemes.UITests/WPF/PopupBox/PopupBoxTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.ComponentModel;
2+
using System.Windows.Media;
3+
using MaterialDesignThemes.UITests.Samples.PopupBox;
24

35
namespace MaterialDesignThemes.UITests.WPF.PopupBox;
46

@@ -32,6 +34,34 @@ public async Task PopupBox_WithElevation_AppliesElevationToNestedCard(Elevation
3234
// Assert
3335
Assert.Equal(elevation, await card.GetProperty<Elevation?>(ElevationAssist.ElevationProperty));
3436

37+
recorder.Success();
38+
}
39+
40+
[Fact]
41+
public async Task PopupBox_WithContentTemplateSelector_ChangesContent()
42+
{
43+
await using var recorder = new TestRecorder(App);
44+
45+
//Arrange
46+
IVisualElement grid = (await LoadUserControl<PopupBoxWithTemplateSelector>());
47+
48+
IVisualElement<Button> button = await grid.GetElement<Button>();
49+
IVisualElement<Wpf.PopupBox> popupBox = await grid.GetElement<Wpf.PopupBox>();
50+
51+
52+
// Assert
53+
var border = await popupBox.GetElement<Border>();
54+
Assert.Equal(Colors.Blue, await border.GetBackgroundColor());
55+
56+
await button.LeftClick();
57+
58+
await Wait.For(async () =>
59+
{
60+
border = await popupBox.GetElement<Border>();
61+
Assert.Equal(Colors.Red, await border.GetBackgroundColor());
62+
});
63+
64+
3565
recorder.Success();
3666
}
3767
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.PopupBox.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
107107
Content="{TemplateBinding Content}"
108108
ContentTemplate="{TemplateBinding ContentTemplate}"
109+
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
109110
Focusable="False"
110111
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
111112
</ControlTemplate>

0 commit comments

Comments
 (0)