Skip to content

Commit 9bbe69c

Browse files
Fixes #3671 - No Case found scenario
1 parent daa8869 commit 9bbe69c

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.ComponentModel;
67
using Windows.UI.Xaml;
78
using Windows.UI.Xaml.Controls;
89
using Windows.UI.Xaml.Markup;
@@ -78,8 +79,12 @@ public Type TargetType
7879
DependencyProperty.Register(nameof(TargetType), typeof(Type), typeof(SwitchPresenter), new PropertyMetadata(null));
7980

8081
/// <summary>
81-
/// Gets or sets a value indicating whether the content is removed from the visual tree when switching between cases.
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"/>.
8286
/// </summary>
87+
[EditorBrowsable(EditorBrowsableState.Never)]
8388
public bool IsVisualTreeDisconnectedOnChange { get; set; }
8489

8590
private static void OnSwitchCasesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -176,14 +181,27 @@ private void EvaluateCases()
176181
// Disconnect old content from visual tree.
177182
if (CurrentCase != null && CurrentCase.Content != null && IsVisualTreeDisconnectedOnChange)
178183
{
179-
// TODO: If we disconnect here, we need to recreate later??? Need to Test...
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.
180188
VisualTreeHelper.DisconnectChildrenRecursive(CurrentCase.Content);
181189
}
182190

183-
// Hookup new content.
184-
Content = newcase.Content;
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;
185195

186-
CurrentCase = newcase;
196+
CurrentCase = null;
197+
}
198+
else
199+
{
200+
// Hookup new content.
201+
Content = newcase.Content;
202+
203+
CurrentCase = newcase;
204+
}
187205
}
188206
}
189207

0 commit comments

Comments
 (0)