Skip to content

Commit 0bfca23

Browse files
Fix issue with ColorPicker not working with SwitchPresenter changes
Since optimizing SwitchPresenter, the delayed loading being in another control's template was causing an issue where the cases weren't available to evaluate when SwitchPresenter was ready. Since we no longer listened to all downstream changes in SwitchPresenter this meant we never evaluated once the parent control was loaded. Added a couple of cases to ensure that once we're fully loaded we can evaluate again to ensure we pick a case. This also covers a case where if somehow the entire collection gets swapped out later. Added some extra checks ahead of the evaluate cases to break out early in case we don't need to update the Visual Tree.
1 parent a66a4a0 commit 0bfca23

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public CaseCollection SwitchCases
4646
/// Indicates the <see cref="SwitchCases"/> property.
4747
/// </summary>
4848
public static readonly DependencyProperty SwitchCasesProperty =
49-
DependencyProperty.Register(nameof(SwitchCases), typeof(CaseCollection), typeof(SwitchPresenter), new PropertyMetadata(null));
49+
DependencyProperty.Register(nameof(SwitchCases), typeof(CaseCollection), typeof(SwitchPresenter), new PropertyMetadata(null, OnSwitchCasesPropertyChanged));
5050

5151
/// <summary>
5252
/// Gets or sets a value indicating the value to compare all cases against. When this value is bound to and changes, the presenter will automatically evaluate cases and select the new appropriate content from the switch.
@@ -81,9 +81,19 @@ public Type TargetType
8181
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
8282
{
8383
// When our Switch's expression changes, re-evaluate.
84-
var xswitch = (SwitchPresenter)d;
84+
if (d is SwitchPresenter xswitch)
85+
{
86+
xswitch.EvaluateCases();
87+
}
88+
}
8589

86-
xswitch.EvaluateCases();
90+
private static void OnSwitchCasesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
91+
{
92+
// If our collection somehow changes, we should re-evaluate.
93+
if (d is SwitchPresenter xswitch)
94+
{
95+
xswitch.EvaluateCases();
96+
}
8797
}
8898

8999
/// <summary>
@@ -92,6 +102,14 @@ private static void OnValuePropertyChanged(DependencyObject d, DependencyPropert
92102
public SwitchPresenter()
93103
{
94104
this.SwitchCases = new CaseCollection();
105+
106+
Loaded += this.SwitchPresenter_Loaded;
107+
}
108+
109+
private void SwitchPresenter_Loaded(object sender, RoutedEventArgs e)
110+
{
111+
// In case we're in a template, we may have loaded cases later.
112+
EvaluateCases();
95113
}
96114

97115
/// <inheritdoc/>
@@ -104,8 +122,20 @@ protected override void OnApplyTemplate()
104122

105123
private void EvaluateCases()
106124
{
107-
if (CurrentCase != null &&
108-
CurrentCase.Value != null &&
125+
if (SwitchCases == null ||
126+
SwitchCases.Count == 0)
127+
{
128+
// If we have no cases, then we can't match anything.
129+
if (CurrentCase != null)
130+
{
131+
// Only bother clearing our actual content if we had something before.
132+
Content = null;
133+
CurrentCase = null;
134+
}
135+
136+
return;
137+
}
138+
else if (CurrentCase?.Value != null &&
109139
CurrentCase.Value.Equals(Value))
110140
{
111141
// If the current case we're on already matches our current value,
@@ -134,7 +164,7 @@ private void EvaluateCases()
134164

135165
if (newcase == null && xdefault != null)
136166
{
137-
// Inject default if we found one.
167+
// Inject default if we found one without matching anything
138168
newcase = xdefault;
139169
}
140170

0 commit comments

Comments
 (0)