From 9bbe69c9488fd9cfcbea0e8f8dc3fda93854b844 Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 25 Feb 2021 12:05:41 -0800 Subject: [PATCH 1/7] Fixes #3671 - No Case found scenario --- .../SwitchPresenter/SwitchPresenter.cs | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs index 0d1202e0bb7..f13e0bd9848 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.ComponentModel; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Markup; @@ -78,8 +79,12 @@ public Type TargetType DependencyProperty.Register(nameof(TargetType), typeof(Type), typeof(SwitchPresenter), new PropertyMetadata(null)); /// - /// Gets or sets a value indicating whether the content is removed from the visual tree when switching between cases. + /// Gets or sets a value indicating whether the content is removed from the visual tree when + /// switching between cases. This calls + /// when switching between cases, so is only meant if the displayed case is never intended to be + /// shown again. No explicit re-initialization of controls is provided by the . /// + [EditorBrowsable(EditorBrowsableState.Never)] public bool IsVisualTreeDisconnectedOnChange { get; set; } private static void OnSwitchCasesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) @@ -176,14 +181,27 @@ private void EvaluateCases() // Disconnect old content from visual tree. if (CurrentCase != null && CurrentCase.Content != null && IsVisualTreeDisconnectedOnChange) { - // TODO: If we disconnect here, we need to recreate later??? Need to Test... + // When we disconnect here, we can't easily redo anything done here, so this is + // an advanced option left open to specific scenarios for a developer. + // It shouldn't come up in normaly usages, unless a developer intends each + // case to only be displayed once. VisualTreeHelper.DisconnectChildrenRecursive(CurrentCase.Content); } - // Hookup new content. - Content = newcase.Content; + // We don't have any cases or a default to go to, so go back to blank + if (newcase == null) + { + Content = null; - CurrentCase = newcase; + CurrentCase = null; + } + else + { + // Hookup new content. + Content = newcase.Content; + + CurrentCase = newcase; + } } } From 2506faacb79200be5bac636367ca4c90cc8b43b8 Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 25 Feb 2021 12:24:52 -0800 Subject: [PATCH 2/7] Add basic SwitchPresenter sample to Sample App --- .../Microsoft.Toolkit.Uwp.SampleApp.csproj | 4 ++ .../Primitives/SwitchPresenter.bind | 47 ++++++++++++++++++ .../Primitives/SwitchPresenter.png | Bin 0 -> 1209 bytes .../SamplePages/XamlOnlyPage.xaml | 6 +++ .../SamplePages/samples.json | 9 ++++ 5 files changed, 66 insertions(+) create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind create mode 100644 Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.png diff --git a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj index 32dda221dcb..a6c0d8c7e51 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj +++ b/Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj @@ -272,6 +272,7 @@ + @@ -976,6 +977,9 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile Designer diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind new file mode 100644 index 00000000000..a69bec0e29b --- /dev/null +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind @@ -0,0 +1,47 @@ + + + + + Select an option + Confirmation Code + E-ticket number + Mileage Plan number + + + + + + Thanks for entering a valid code! + + + + + + Thanks for entering a valid code! + + + + + + + Please select a way to lookup your reservation above... + + + + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.png b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.png new file mode 100644 index 0000000000000000000000000000000000000000..16ae1b9188731eed7e36c1069ce7043b4a40c185 GIT binary patch literal 1209 zcmeAS@N?(olHy`uVBq!ia0y~yU{nCI4{)#n$t4^1Z39w_#X;^)4C~IxyaaMs(j9#r z85lP9bN@+X1@buyJR*x382Ao@Fyrz36)8Y9ex5FlAr*0N?;2*^4v;xsXeq!GXH=#k zTiZ~^@xyMXBj1GPCoDTVm&Z9IUv_vo?P#0Boju|^H*e^voA&qTfyXDK7d$;3@2_xn z*0P`fHZr(v=f5!j7k7e9{JLG-4Hr2D1e6^ddKefDnOKf+P=RCoZTZala%-NiTW`+!%LHyaPSF3jJ+xM>S>7KoNP2azNe|LSeMaw+9_6rVuoG+ZF zoLnBUHtcNL=9SksNB(?%e#ZIdXRG$EyUyJNa_C zM(~Y)`Hzagz7StzQ>wLk&fYg=yLU|z>puE_f9L6^0>2Jb@SNu?T0~4J^U-$x8=6ZO8#W` z+W#Iq%M(Np@-3_`;Fue3ry0A%UVHEVX?xFm$+tRZC>&?G(#)xmZvS&l*y>&RX0vyl zPkXxm-H&IVEAL)^U3!;)PK%Gjd>;pgK8C_er)Gk^y3Xk5l4bK3`6Q&i{jb+1sNff6 z`9*j2onKS7-hTVaD^f?S6e6y0OtFNYZ{cT~HhXWrg^e=80sLwTeP>v{oMeH!%y>av z%damyRXkI#Yzm^Cbe-@SYHuKRcHT&d0J@9!_Iuc%n@n{f^! z6a&v)IwckkL?1~r8lvx;5^Uwi#JJ66-*-@kOO5YT;hn!Y{| zRAElaB4BaD;OXk; Jvd$@?2>@R=xuXC8 literal 0 HcmV?d00001 diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml index dee42559784..8577def2b8d 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml @@ -3,6 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ani="using:Microsoft.Toolkit.Uwp.UI.Animations" xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors" + xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:interactions="using:Microsoft.Xaml.Interactions.Core" @@ -34,6 +35,11 @@ + + + + + diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json index f5a819be31a..826637d36e2 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json @@ -463,6 +463,15 @@ "XamlCodeFile": "/SamplePages/TabbedCommandBar/TabbedCommandBar.bind", "Icon": "/SamplePages/TabbedCommandBar/TabbedCommandBar.png", "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/controls/TabbedCommandBar.md" + }, + { + "Name": "SwitchPresenter", + "Subcategory": "Layout", + "About": "The SwitchPresenter is a ContentPresenter which can allow a developer to mimic a switch statement within XAML.", + "CodeUrl": "https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/master/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter", + "XamlCodeFile": "/SamplePages/Primitives/SwitchPresenter.bind", + "Icon": "/SamplePages/Primitives/SwitchPresenter.png", + "DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/controls/SwitchPresenter.md" } ] }, From c63e6955ba718b6edf5171282b50d2c2a5bce60c Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Mon, 1 Mar 2021 12:55:05 -0800 Subject: [PATCH 3/7] Remove complex property from SwitchPresenter Fix name of TargetType DependencyProperty Add better Enum string support --- .../Primitives/SwitchPresenter.bind | 26 ++++++++- .../SwitchPresenter/SwitchPresenter.cs | 58 +++++++------------ 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind index a69bec0e29b..1e0607690b7 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind @@ -5,9 +5,11 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" xmlns:ui="using:Microsoft.Toolkit.Uwp.UI" + xmlns:enums="using:Microsoft.Toolkit.Uwp.SampleApp.Enums" mc:Ignorable="d"> + Select an option @@ -15,14 +17,16 @@ E-ticket number Mileage Plan number + + - Thanks for entering a valid code! + Thanks for entering a valid code! @@ -31,7 +35,7 @@ ui:TextBoxExtensions.Regex="(^\d{10}$)|(^\d{13}$)" Header="E-ticket number" PlaceholderText="10 or 13 numbers" /> - Thanks for entering a valid code! + Thanks for entering a valid code! @@ -43,5 +47,23 @@ Please select a way to lookup your reservation above... + + + + + + 🐈 + + + 🐕 + + + 🐇 + + diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs index f13e0bd9848..814704602ba 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs @@ -68,25 +68,16 @@ public object Value /// public Type TargetType { - get { return (Type)GetValue(DataTypeProperty); } - set { SetValue(DataTypeProperty, value); } + get { return (Type)GetValue(TargetTypeProperty); } + set { SetValue(TargetTypeProperty, value); } } /// /// Indicates the property. /// - public static readonly DependencyProperty DataTypeProperty = + public static readonly DependencyProperty TargetTypeProperty = DependencyProperty.Register(nameof(TargetType), typeof(Type), typeof(SwitchPresenter), new PropertyMetadata(null)); - /// - /// Gets or sets a value indicating whether the content is removed from the visual tree when - /// switching between cases. This calls - /// when switching between cases, so is only meant if the displayed case is never intended to be - /// shown again. No explicit re-initialization of controls is provided by the . - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public bool IsVisualTreeDisconnectedOnChange { get; set; } - private static void OnSwitchCasesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.OldValue != null) @@ -175,33 +166,12 @@ private void EvaluateCases() newcase = xdefault; } - // Only bother changing things around if we have a new case. + // Only bother changing things around if we actually have a new case. if (newcase != CurrentCase) { - // Disconnect old content from visual tree. - if (CurrentCase != null && CurrentCase.Content != null && IsVisualTreeDisconnectedOnChange) - { - // When we disconnect here, we can't easily redo anything done here, so this is - // an advanced option left open to specific scenarios for a developer. - // It shouldn't come up in normaly usages, unless a developer intends each - // case to only be displayed once. - VisualTreeHelper.DisconnectChildrenRecursive(CurrentCase.Content); - } - - // We don't have any cases or a default to go to, so go back to blank - if (newcase == null) - { - Content = null; - - CurrentCase = null; - } - else - { - // Hookup new content. - Content = newcase.Content; - - CurrentCase = newcase; - } + // If we don't have any cases or default, setting these to null is what we want to be blank again. + Content = newcase?.Content; + CurrentCase = newcase; } } @@ -255,6 +225,20 @@ internal static object ConvertValue(Type targetType, object value) { return value; } + else if (targetType.IsEnum && value is string str) + { + if (Enum.TryParse(targetType, str, out object result)) + { + return result; + } + + static object ThrowExceptionForKeyNotFound() + { + throw new InvalidOperationException("The requested enum value was not present in the provided type."); + } + + return ThrowExceptionForKeyNotFound(); + } else { return XamlBindingHelper.ConvertValue(targetType, value); From 332fd2c2b441f6691e997951cdc080daba240ede Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 4 Mar 2021 11:24:58 -0800 Subject: [PATCH 4/7] Remove duplicate style from WrapPanel sample --- .../SamplePages/WrapPanel/WrapPanel.bind | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/WrapPanel/WrapPanel.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/WrapPanel/WrapPanel.bind index bb3f26c22d7..bd338f55473 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/WrapPanel/WrapPanel.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/WrapPanel/WrapPanel.bind @@ -32,8 +32,13 @@ @@ -49,16 +54,6 @@ HorizontalSpacing="@[HorizontalSpacing:Slider:5:0-200]@" /> - - - \ No newline at end of file From 08bbe5802cb9809ee87d800efeb76786121cee96 Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 4 Mar 2021 12:32:55 -0800 Subject: [PATCH 5/7] Clean-up sample and better separate the two examples --- .../Primitives/SwitchPresenter.bind | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind index 1e0607690b7..4c3afe51e61 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind @@ -19,7 +19,7 @@ - + + Please select a way to lookup your reservation above... - - + + + + + TargetType="enums:Animal" + Padding="16"> - 🐈 + 🐈 - 🐕 + 🐕 - 🐇 + 🐇 + + + 🦜 + + + 🐿 From 2e3cceca76175ec38b93f4c0691dfaec037f481a Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 4 Mar 2021 12:35:56 -0800 Subject: [PATCH 6/7] Remove SwitchPresenter logic that would listen to changes in the collection or case values Can't think of scenarios where this was actually useful and added a lot of complexity. Removing it for now for the core value the control provides and simplifies the structure. Samples from Sample App continue to work as expected. --- .../SwitchPresenter/Case.cs | 16 +-- .../SwitchPresenter/CaseCollection.cs | 114 +----------------- .../SwitchPresenter/SwitchPresenter.cs | 34 +----- 3 files changed, 6 insertions(+), 158 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/Case.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/Case.cs index 7829abc2a8f..bc7073d9ab5 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/Case.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/Case.cs @@ -14,13 +14,6 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls [ContentProperty(Name = nameof(Content))] public partial class Case : DependencyObject { - internal SwitchPresenter Parent { get; set; } // TODO: Can we remove Parent need here and just use events? - - /// - /// Event raised when the property changes. - /// - public event EventHandler ValueChanged; - /// /// Gets or sets the Content to display when this case is active. /// @@ -64,14 +57,7 @@ public object Value /// Identifies the property. /// public static readonly DependencyProperty ValueProperty = - DependencyProperty.Register(nameof(Value), typeof(object), typeof(Case), new PropertyMetadata(null, OnValuePropertyChanged)); - - private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - var xcase = (Case)d; - - xcase.ValueChanged?.Invoke(xcase, EventArgs.Empty); - } + DependencyProperty.Register(nameof(Value), typeof(object), typeof(Case), new PropertyMetadata(null)); /// /// Initializes a new instance of the class. diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/CaseCollection.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/CaseCollection.cs index d1f3ee96187..42c8a9f7e80 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/CaseCollection.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/CaseCollection.cs @@ -5,130 +5,20 @@ using System; using System.Collections; using System.Collections.Generic; +using Windows.UI.Xaml; namespace Microsoft.Toolkit.Uwp.UI.Controls { /// /// An collection of to help with XAML interop. /// - public class CaseCollection : IList, IEnumerable // TODO: Do we need this or can we use an ObservableCollection directly??? (Or is it useful to have it manage the registration of the child events?) + public class CaseCollection : DependencyObjectCollection { - internal SwitchPresenter Parent { get; set; } // TODO: Can we remove Parent need here and just use events? - - private readonly List _internalList = new List(); - - /// - public int Count => _internalList.Count; - - /// - public bool IsReadOnly => false; - - /// - public Case this[int index] { get => _internalList[index]; set => Insert(index, value); } - - /// - /// Raised when an animation has been added/removed or modified - /// - public event EventHandler CaseCollectionChanged; - - private void ValueChanged(object sender, EventArgs e) - { - CaseCollectionChanged?.Invoke(this, EventArgs.Empty); - } - /// /// Initializes a new instance of the class. /// public CaseCollection() { } - - /// - public int IndexOf(Case item) - { - return _internalList.IndexOf(item); - } - - /// - public void Insert(int index, Case item) - { - item.ValueChanged += ValueChanged; - item.Parent = Parent; - _internalList.Insert(index, item); - CaseCollectionChanged?.Invoke(this, EventArgs.Empty); - } - - /// - public void RemoveAt(int index) - { - if (index >= 0 && index < _internalList.Count) - { - var xcase = _internalList[index]; - xcase.ValueChanged -= ValueChanged; - xcase.Parent = null; - } - - _internalList.RemoveAt(index); - CaseCollectionChanged?.Invoke(this, EventArgs.Empty); - } - - /// - public void Add(Case item) - { - item.ValueChanged += ValueChanged; - item.Parent = Parent; - _internalList.Add(item); - CaseCollectionChanged?.Invoke(this, EventArgs.Empty); - } - - /// - public void Clear() - { - foreach (var xcase in _internalList) - { - xcase.ValueChanged -= ValueChanged; - xcase.Parent = null; - } - - _internalList.Clear(); - CaseCollectionChanged?.Invoke(this, EventArgs.Empty); - } - - /// - public bool Contains(Case item) - { - return _internalList.Contains(item); - } - - /// - public void CopyTo(Case[] array, int arrayIndex) - { - _internalList.CopyTo(array, arrayIndex); - } - - /// - public bool Remove(Case item) - { - var result = _internalList.Remove(item); - if (result) - { - item.ValueChanged -= ValueChanged; - item.Parent = null; - CaseCollectionChanged?.Invoke(this, EventArgs.Empty); - } - - return result; - } - - /// - public IEnumerator GetEnumerator() - { - return _internalList.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return _internalList.GetEnumerator(); - } } } diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs index 814704602ba..c977b1b3681 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/SwitchPresenter/SwitchPresenter.cs @@ -46,7 +46,7 @@ public CaseCollection SwitchCases /// Indicates the property. /// public static readonly DependencyProperty SwitchCasesProperty = - DependencyProperty.Register(nameof(SwitchCases), typeof(CaseCollection), typeof(SwitchPresenter), new PropertyMetadata(null, new PropertyChangedCallback(OnSwitchCasesPropertyChanged))); + DependencyProperty.Register(nameof(SwitchCases), typeof(CaseCollection), typeof(SwitchPresenter), new PropertyMetadata(null)); /// /// 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. @@ -61,7 +61,7 @@ public object Value /// Indicates the property. /// public static readonly DependencyProperty ValueProperty = - DependencyProperty.Register(nameof(Value), typeof(object), typeof(SwitchPresenter), new PropertyMetadata(null, new PropertyChangedCallback(OnValuePropertyChanged))); + DependencyProperty.Register(nameof(Value), typeof(object), typeof(SwitchPresenter), new PropertyMetadata(null, OnValuePropertyChanged)); /// /// Gets or sets a value indicating which type to first cast and compare provided values against. @@ -78,26 +78,6 @@ public Type TargetType public static readonly DependencyProperty TargetTypeProperty = DependencyProperty.Register(nameof(TargetType), typeof(Type), typeof(SwitchPresenter), new PropertyMetadata(null)); - private static void OnSwitchCasesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - if (e.OldValue != null) - { - ((SwitchPresenter)e.OldValue).SwitchCases.CaseCollectionChanged -= OnCaseValuePropertyChanged; - } - - var xswitch = (SwitchPresenter)d; - - foreach (var xcase in xswitch.SwitchCases) - { - // Set our parent - xcase.Parent = xswitch; - } - - // Will trigger on collection change and case value changed - xswitch.SwitchCases.Parent = xswitch; - xswitch.SwitchCases.CaseCollectionChanged += OnCaseValuePropertyChanged; - } - private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // When our Switch's expression changes, re-evaluate. @@ -106,14 +86,6 @@ private static void OnValuePropertyChanged(DependencyObject d, DependencyPropert xswitch.EvaluateCases(); } - private static void OnCaseValuePropertyChanged(object sender, EventArgs e) - { - // When something about our collection of cases changes, re-evaluate. - var collection = (CaseCollection)sender; - - collection.Parent.EvaluateCases(); - } - /// /// Initializes a new instance of the class. /// @@ -144,7 +116,7 @@ private void EvaluateCases() Case xdefault = null; Case newcase = null; - foreach (var xcase in SwitchCases) + foreach (Case xcase in SwitchCases) { if (xcase.IsDefault) { From bb961a4d8988285378c738ecd586b64f408eceaa Mon Sep 17 00:00:00 2001 From: michael-hawker <24302614+michael-hawker@users.noreply.github.com> Date: Thu, 4 Mar 2021 13:02:06 -0800 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=A6=99=20Add=20more=20=F0=9F=A6=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SamplePages/EnumValuesExtension/Animal.cs | 1 + .../EnumValuesExtension/AnimalToColorConverter.xaml.cs | 1 + .../SamplePages/Primitives/SwitchPresenter.bind | 3 +++ 3 files changed, 5 insertions(+) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/Animal.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/Animal.cs index 2cf8d744e6c..166c1d4791e 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/Animal.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/Animal.cs @@ -9,6 +9,7 @@ public enum Animal Cat, Dog, Bunny, + Llama, Parrot, Squirrel } diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/AnimalToColorConverter.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/AnimalToColorConverter.xaml.cs index 0b3cf01b79c..a681c092e75 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/AnimalToColorConverter.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/EnumValuesExtension/AnimalToColorConverter.xaml.cs @@ -18,6 +18,7 @@ public object Convert(object value, Type targetType, object parameter, string la Animal.Cat => Colors.Coral, Animal.Dog => Colors.Gray, Animal.Bunny => Colors.Green, + Animal.Llama => Colors.Beige, Animal.Parrot => Colors.YellowGreen, Animal.Squirrel => Colors.SaddleBrown, _ => throw new ArgumentException("Invalid value", nameof(value)) diff --git a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind index 4c3afe51e61..26e73185dfe 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind +++ b/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/SwitchPresenter.bind @@ -68,6 +68,9 @@ 🐇 + + 🦙 + 🦜