Skip to content

Commit c63e695

Browse files
Remove complex property from SwitchPresenter
Fix name of TargetType DependencyProperty Add better Enum string support
1 parent 2506faa commit c63e695

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
77
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
8+
xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums"
89
mc:Ignorable="d">
910

1011
<StackPanel Padding="16">
12+
<!-- Basic Sample -->
1113
<ComboBox x:Name="Lookup" Header="Look up reservation" SelectedIndex="0"
1214
Margin="0,0,0,8">
1315
<x:String>Select an option</x:String>
1416
<x:String>Confirmation Code</x:String>
1517
<x:String>E-ticket number</x:String>
1618
<x:String>Mileage Plan number</x:String>
1719
</ComboBox>
20+
<!-- SwitchPresenter binds to a value -->
1821
<controls:SwitchPresenter Value="{Binding SelectedItem, ElementName=Lookup}">
22+
<!-- And then only dynamically displays the Case with matching Value -->
1923
<controls:Case Value="Confirmation Code">
2024
<StackPanel>
2125
<TextBox Name="ConfirmationCodeValidator"
2226
ui:TextBoxExtensions.Regex="^[a-zA-Z]{6}$"
2327
Header="Confirmation code"
2428
PlaceholderText="6 letters" />
25-
<TextBlock Visibility="{Binding (ui:TextBoxExtensions.IsValid),ElementName=ConfirmationCodeValidator}">Thanks for entering a valid code!</TextBlock>
29+
<TextBlock Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=ConfirmationCodeValidator}">Thanks for entering a valid code!</TextBlock>
2630
</StackPanel>
2731
</controls:Case>
2832
<controls:Case Value="E-ticket number">
@@ -31,7 +35,7 @@
3135
ui:TextBoxExtensions.Regex="(^\d{10}$)|(^\d{13}$)"
3236
Header="E-ticket number"
3337
PlaceholderText="10 or 13 numbers" />
34-
<TextBlock Visibility="{Binding (ui:TextBoxExtensions.IsValid),ElementName=TicketValidator}">Thanks for entering a valid code!</TextBlock>
38+
<TextBlock Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=TicketValidator}">Thanks for entering a valid code!</TextBlock>
3539
</StackPanel>
3640
</controls:Case>
3741
<controls:Case Value="Mileage Plan number">
@@ -43,5 +47,23 @@
4347
<TextBlock>Please select a way to lookup your reservation above...</TextBlock>
4448
</controls:Case>
4549
</controls:SwitchPresenter>
50+
51+
<!-- Advanced scenario using Type information -->
52+
<ComboBox x:Name="AnimalPicker"
53+
Header="Pick an Animal"
54+
ItemsSource="{ui:EnumValues Type=enums:Animal}"
55+
SelectedIndex="0"/>
56+
<controls:SwitchPresenter Value="{Binding SelectedItem, ElementName=AnimalPicker}"
57+
TargetType="enums:Animal">
58+
<controls:Case Value="Cat">
59+
<TextBlock>🐈</TextBlock>
60+
</controls:Case>
61+
<controls:Case Value="Dog">
62+
<TextBlock>🐕</TextBlock>
63+
</controls:Case>
64+
<controls:Case Value="Bunny">
65+
<TextBlock>🐇</TextBlock>
66+
</controls:Case>
67+
</controls:SwitchPresenter>
4668
</StackPanel>
4769
</Page>

Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,16 @@ public object Value
6868
/// </summary>
6969
public Type TargetType
7070
{
71-
get { return (Type)GetValue(DataTypeProperty); }
72-
set { SetValue(DataTypeProperty, value); }
71+
get { return (Type)GetValue(TargetTypeProperty); }
72+
set { SetValue(TargetTypeProperty, value); }
7373
}
7474

7575
/// <summary>
7676
/// Indicates the <see cref="TargetType"/> property.
7777
/// </summary>
78-
public static readonly DependencyProperty DataTypeProperty =
78+
public static readonly DependencyProperty TargetTypeProperty =
7979
DependencyProperty.Register(nameof(TargetType), typeof(Type), typeof(SwitchPresenter), new PropertyMetadata(null));
8080

81-
/// <summary>
82-
/// Gets or sets a value indicating whether the content is removed from the visual tree when
83-
/// switching between cases. This calls <see cref="VisualTreeHelper.DisconnectChildrenRecursive(UIElement)"/>
84-
/// when switching between cases, so is only meant if the displayed case is never intended to be
85-
/// shown again. No explicit re-initialization of controls is provided by the <see cref="SwitchPresenter"/>.
86-
/// </summary>
87-
[EditorBrowsable(EditorBrowsableState.Never)]
88-
public bool IsVisualTreeDisconnectedOnChange { get; set; }
89-
9081
private static void OnSwitchCasesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
9182
{
9283
if (e.OldValue != null)
@@ -175,33 +166,12 @@ private void EvaluateCases()
175166
newcase = xdefault;
176167
}
177168

178-
// Only bother changing things around if we have a new case.
169+
// Only bother changing things around if we actually have a new case.
179170
if (newcase != CurrentCase)
180171
{
181-
// Disconnect old content from visual tree.
182-
if (CurrentCase != null && CurrentCase.Content != null && IsVisualTreeDisconnectedOnChange)
183-
{
184-
// When we disconnect here, we can't easily redo anything done here, so this is
185-
// an advanced option left open to specific scenarios for a developer.
186-
// It shouldn't come up in normaly usages, unless a developer intends each
187-
// case to only be displayed once.
188-
VisualTreeHelper.DisconnectChildrenRecursive(CurrentCase.Content);
189-
}
190-
191-
// We don't have any cases or a default to go to, so go back to blank
192-
if (newcase == null)
193-
{
194-
Content = null;
195-
196-
CurrentCase = null;
197-
}
198-
else
199-
{
200-
// Hookup new content.
201-
Content = newcase.Content;
202-
203-
CurrentCase = newcase;
204-
}
172+
// If we don't have any cases or default, setting these to null is what we want to be blank again.
173+
Content = newcase?.Content;
174+
CurrentCase = newcase;
205175
}
206176
}
207177

@@ -255,6 +225,20 @@ internal static object ConvertValue(Type targetType, object value)
255225
{
256226
return value;
257227
}
228+
else if (targetType.IsEnum && value is string str)
229+
{
230+
if (Enum.TryParse(targetType, str, out object result))
231+
{
232+
return result;
233+
}
234+
235+
static object ThrowExceptionForKeyNotFound()
236+
{
237+
throw new InvalidOperationException("The requested enum value was not present in the provided type.");
238+
}
239+
240+
return ThrowExceptionForKeyNotFound();
241+
}
258242
else
259243
{
260244
return XamlBindingHelper.ConvertValue(targetType, value);

0 commit comments

Comments
 (0)