Skip to content

Commit bfc2c90

Browse files
authored
Merge pull request #417 from YourOrdinaryCat/feature/expander-template-selection
[SettingsControls] Switch to object type for `ItemTemplate` property
2 parents d95a2d6 + 901ff7e commit bfc2c90

File tree

3 files changed

+119
-45
lines changed

3 files changed

+119
-45
lines changed

components/SettingsControls/samples/SettingsExpanderItemsSourceSample.xaml

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,84 @@
99
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
1010
mc:Ignorable="d">
1111

12-
<labs:SettingsExpander Description="The SettingsExpander can use ItemsSource to define its Items."
13-
Header="Settings Expander with ItemsSource"
14-
ItemsSource="{x:Bind MyDataSet}">
15-
<labs:SettingsExpander.HeaderIcon>
16-
<FontIcon Glyph="&#xEA37;" />
17-
</labs:SettingsExpander.HeaderIcon>
18-
<labs:SettingsExpander.ItemTemplate>
19-
<DataTemplate x:DataType="local:MyDataModel">
20-
<labs:SettingsCard Description="{x:Bind Info}"
21-
Header="{x:Bind Name}">
22-
<HyperlinkButton Content="{x:Bind LinkDescription}"
23-
NavigateUri="{x:Bind Url}" />
24-
</labs:SettingsCard>
25-
</DataTemplate>
26-
</labs:SettingsExpander.ItemTemplate>
27-
<labs:SettingsExpander.ItemsHeader>
28-
<muxc:InfoBar Title="This is the ItemsHeader"
29-
BorderThickness="0"
30-
CornerRadius="0"
31-
IsIconVisible="False"
32-
IsOpen="True"
33-
Severity="Success">
34-
<muxc:InfoBar.ActionButton>
35-
<HyperlinkButton Content="It can host custom content" />
36-
</muxc:InfoBar.ActionButton>
37-
</muxc:InfoBar>
38-
</labs:SettingsExpander.ItemsHeader>
39-
<labs:SettingsExpander.ItemsFooter>
40-
<muxc:InfoBar Title="This is the ItemsFooter"
41-
BorderThickness="0"
42-
CornerRadius="0,0,4,4"
43-
IsIconVisible="False"
44-
IsOpen="True"
45-
Severity="Informational">
46-
<muxc:InfoBar.ActionButton>
47-
<HyperlinkButton Content="It can host custom content" />
48-
</muxc:InfoBar.ActionButton>
49-
</muxc:InfoBar>
50-
</labs:SettingsExpander.ItemsFooter>
51-
</labs:SettingsExpander>
12+
<StackPanel Spacing="3">
13+
<labs:SettingsExpander Description="The SettingsExpander can use ItemsSource to define its Items."
14+
Header="Settings Expander with ItemsSource"
15+
ItemsSource="{x:Bind MyDataSet}">
16+
<labs:SettingsExpander.HeaderIcon>
17+
<FontIcon Glyph="&#xEA37;" />
18+
</labs:SettingsExpander.HeaderIcon>
19+
<labs:SettingsExpander.ItemTemplate>
20+
<DataTemplate x:DataType="local:MyDataModel">
21+
<labs:SettingsCard Description="{x:Bind Info}"
22+
Header="{x:Bind Name}">
23+
<HyperlinkButton Content="{x:Bind LinkDescription}"
24+
NavigateUri="{x:Bind Url}" />
25+
</labs:SettingsCard>
26+
</DataTemplate>
27+
</labs:SettingsExpander.ItemTemplate>
28+
<labs:SettingsExpander.ItemsHeader>
29+
<muxc:InfoBar Title="This is the ItemsHeader"
30+
BorderThickness="0"
31+
CornerRadius="0"
32+
IsIconVisible="False"
33+
IsOpen="True"
34+
Severity="Success">
35+
<muxc:InfoBar.ActionButton>
36+
<HyperlinkButton Content="It can host custom content" />
37+
</muxc:InfoBar.ActionButton>
38+
</muxc:InfoBar>
39+
</labs:SettingsExpander.ItemsHeader>
40+
<labs:SettingsExpander.ItemsFooter>
41+
<muxc:InfoBar Title="This is the ItemsFooter"
42+
BorderThickness="0"
43+
CornerRadius="0,0,4,4"
44+
IsIconVisible="False"
45+
IsOpen="True"
46+
Severity="Informational">
47+
<muxc:InfoBar.ActionButton>
48+
<HyperlinkButton Content="It can host custom content" />
49+
</muxc:InfoBar.ActionButton>
50+
</muxc:InfoBar>
51+
</labs:SettingsExpander.ItemsFooter>
52+
</labs:SettingsExpander>
53+
54+
<labs:SettingsExpander Description="SettingsExpander can use a DataTemplate, DataTemplateSelector, or IElementFactory for its ItemTemplate."
55+
Header="Settings Expander with a custom ItemTemplate"
56+
ItemsSource="{x:Bind MyDataSet}">
57+
<labs:SettingsExpander.HeaderIcon>
58+
<FontIcon Glyph="&#xEA37;" />
59+
</labs:SettingsExpander.HeaderIcon>
60+
<labs:SettingsExpander.ItemTemplate>
61+
<local:MyDataModelTemplateSelector>
62+
<local:MyDataModelTemplateSelector.ButtonTemplate>
63+
<DataTemplate x:DataType="local:MyDataModel">
64+
<labs:SettingsCard Description="{x:Bind ItemType}"
65+
Header="{x:Bind Name}">
66+
<Button Click="Button_Click"
67+
Content="{x:Bind LinkDescription}" />
68+
</labs:SettingsCard>
69+
</DataTemplate>
70+
</local:MyDataModelTemplateSelector.ButtonTemplate>
71+
72+
<local:MyDataModelTemplateSelector.LinkButtonTemplate>
73+
<DataTemplate x:DataType="local:MyDataModel">
74+
<labs:SettingsCard Description="{x:Bind ItemType}"
75+
Header="{x:Bind Name}">
76+
<HyperlinkButton Content="{x:Bind LinkDescription}"
77+
NavigateUri="{x:Bind Url}" />
78+
</labs:SettingsCard>
79+
</DataTemplate>
80+
</local:MyDataModelTemplateSelector.LinkButtonTemplate>
81+
82+
<local:MyDataModelTemplateSelector.NoButtonTemplate>
83+
<DataTemplate x:DataType="local:MyDataModel">
84+
<labs:SettingsCard Description="{x:Bind ItemType}"
85+
Header="{x:Bind Name}" />
86+
</DataTemplate>
87+
</local:MyDataModelTemplateSelector.NoButtonTemplate>
88+
</local:MyDataModelTemplateSelector>
89+
</labs:SettingsExpander.ItemTemplate>
90+
</labs:SettingsExpander>
91+
</StackPanel>
5292
</Page>

components/SettingsControls/samples/SettingsExpanderItemsSourceSample.xaml.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Collections.ObjectModel;
5+
using Windows.System;
66

77
namespace SettingsControlsExperiment.Samples;
88

99
[ToolkitSample(id: nameof(SettingsExpanderItemsSourceSample), "SettingsExpanderItemsSource", description: "The SettingsExpander can also be filled with items based on a collection.")]
1010
public sealed partial class SettingsExpanderItemsSourceSample : Page
1111
{
1212

13-
public ObservableCollection<MyDataModel> MyDataSet = new () {
13+
public ObservableCollection<MyDataModel> MyDataSet = new() {
1414
new()
1515
{
1616
Name = "First Item",
1717
Info = "More about first item.",
18+
ItemType = "Item type: Button",
1819
LinkDescription = "Click here for more on first item.",
1920
Url = "https://microsoft.com/",
2021
},
2122
new()
2223
{
2324
Name = "Second Item",
2425
Info = "More about second item.",
26+
ItemType = "Item type: Link button",
2527
LinkDescription = "Click here for more on second item.",
2628
Url = "https://xbox.com/",
2729
},
2830
new()
2931
{
3032
Name = "Third Item",
3133
Info = "More about third item.",
34+
ItemType = "Item type: No button",
3235
LinkDescription = "Click here for more on third item.",
3336
Url = "https://toolkitlabs.dev/",
3437
},
@@ -38,6 +41,11 @@ public SettingsExpanderItemsSourceSample()
3841
{
3942
this.InitializeComponent();
4043
}
44+
45+
private async void Button_Click(object sender, RoutedEventArgs e)
46+
{
47+
_ = await Launcher.LaunchUriAsync(new("https://microsoft.com/"));
48+
}
4149
}
4250

4351
public class MyDataModel
@@ -46,7 +54,33 @@ public class MyDataModel
4654

4755
public string? Info { get; set; }
4856

57+
public string? ItemType { get; set; }
58+
4959
public string? LinkDescription { get; set; }
5060

5161
public string? Url { get; set; }
5262
}
63+
64+
public class MyDataModelTemplateSelector : DataTemplateSelector
65+
{
66+
public DataTemplate? ButtonTemplate { get; set; }
67+
public DataTemplate? LinkButtonTemplate { get; set; }
68+
public DataTemplate? NoButtonTemplate { get; set; }
69+
70+
protected override DataTemplate SelectTemplateCore(object item)
71+
{
72+
var itm = (MyDataModel)item;
73+
if (itm.ItemType?.EndsWith("Button") == true)
74+
{
75+
return ButtonTemplate!;
76+
}
77+
else if (itm.ItemType?.EndsWith("Link button") == true)
78+
{
79+
return LinkButtonTemplate!;
80+
}
81+
else
82+
{
83+
return NoButtonTemplate!;
84+
}
85+
}
86+
}

components/SettingsControls/src/SettingsExpander/SettingsExpander.ItemsControl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public object ItemsSource
2424
public static readonly DependencyProperty ItemsSourceProperty =
2525
DependencyProperty.Register(nameof(ItemsSource), typeof(object), typeof(SettingsExpander), new PropertyMetadata(null, OnItemsConnectedPropertyChanged));
2626

27-
public DataTemplate ItemTemplate
27+
public object ItemTemplate
2828
{
29-
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
29+
get { return (object)GetValue(ItemTemplateProperty); }
3030
set { SetValue(ItemTemplateProperty, value); }
3131
}
3232

3333
public static readonly DependencyProperty ItemTemplateProperty =
34-
DependencyProperty.Register(nameof(ItemTemplate), typeof(DataTemplate), typeof(SettingsExpander), new PropertyMetadata(null));
34+
DependencyProperty.Register(nameof(ItemTemplate), typeof(object), typeof(SettingsExpander), new PropertyMetadata(null));
3535

3636
public StyleSelector ItemContainerStyleSelector
3737
{

0 commit comments

Comments
 (0)