@@ -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