From 4e4a3d86cc9df2417e5cb36e3e366fb51f117f5a Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Fri, 22 Feb 2019 22:43:20 +0300 Subject: [PATCH] use a boolean property instead of a char property for backward-compatibility --- .../TextBoxMask/TextBoxMask.Properties.cs | 18 ++++---- .../Extensions/TextBoxMask/TextBoxMask.cs | 41 ++++++++++--------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/TextBoxMask/TextBoxMask.Properties.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/TextBoxMask/TextBoxMask.Properties.cs index 4d22cba66..c4bf6ad96 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/TextBoxMask/TextBoxMask.Properties.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/TextBoxMask/TextBoxMask.Properties.cs @@ -30,10 +30,10 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions public static readonly DependencyProperty CustomMaskProperty = DependencyProperty.RegisterAttached("CustomMask", typeof(string), typeof(TextBoxMask), new PropertyMetadata(null, InitTextBoxMask)); /// - /// Represents the character that's used to escape variables in the mask + /// Represents whether the variables in the mask can be escaped /// - public static readonly DependencyProperty EscapeCharacterProperty = - DependencyProperty.RegisterAttached("EscapeCharacter", typeof(string), typeof(TextBoxMask), new PropertyMetadata("\0")); + public static readonly DependencyProperty EscapeVariablesProperty = + DependencyProperty.RegisterAttached("EscapeVariables", typeof(bool), typeof(TextBoxMask), new PropertyMetadata(false, InitTextBoxMask)); private static readonly DependencyProperty RepresentationDictionaryProperty = DependencyProperty.RegisterAttached("RepresentationDictionary", typeof(Dictionary), typeof(TextBoxMask), new PropertyMetadata(null)); private static readonly DependencyProperty OldTextProperty = DependencyProperty.RegisterAttached("OldText", typeof(string), typeof(TextBoxMask), new PropertyMetadata(null)); @@ -42,7 +42,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions private static readonly DependencyProperty OldSelectionStartProperty = DependencyProperty.RegisterAttached("OldSelectionStart", typeof(int), typeof(TextBoxMask), new PropertyMetadata(0)); private static readonly DependencyProperty EscapedMaskProperty = DependencyProperty.RegisterAttached("EscapedMask", typeof(string), typeof(TextBoxMask), new PropertyMetadata(null)); - private static readonly DependencyProperty MaskEscapedCharIndecesProperty = DependencyProperty.RegisterAttached("MaskEscapedCharIndeces", typeof(List), typeof(TextBoxMask), new PropertyMetadata(null)); + private static readonly DependencyProperty MaskEscapedCharactersProperty = DependencyProperty.RegisterAttached("MaskEscapedCharacters", typeof(List), typeof(TextBoxMask), new PropertyMetadata(null)); /// /// Gets mask value @@ -109,19 +109,19 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions /// /// /// - public static string GetEscapeCharacter(DependencyObject obj) + public static bool GetEscapeVariables(DependencyObject obj) { - return (string)obj.GetValue(EscapeCharacterProperty); + return (bool)obj.GetValue(EscapeVariablesProperty); } /// - /// Sets EscapeCharacter property which represents the character that's used to escape variables in the mask + /// Sets EscapeCharacter property which determines whether the variables in the mask can be escaped /// /// /// - public static void SetEscapeCharacter(DependencyObject obj, string value) + public static void SetEscapeVariables(DependencyObject obj, bool value) { - obj.SetValue(EscapeCharacterProperty, value); + obj.SetValue(EscapeVariablesProperty, value); } } } diff --git a/Microsoft.Toolkit.Uwp.UI/Extensions/TextBoxMask/TextBoxMask.cs b/Microsoft.Toolkit.Uwp.UI/Extensions/TextBoxMask/TextBoxMask.cs index fd824b3bf..89b68b1bd 100644 --- a/Microsoft.Toolkit.Uwp.UI/Extensions/TextBoxMask/TextBoxMask.cs +++ b/Microsoft.Toolkit.Uwp.UI/Extensions/TextBoxMask/TextBoxMask.cs @@ -18,6 +18,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions public partial class TextBoxMask { private const string DefaultPlaceHolder = "_"; + private const char EscapeChar = '\\'; private static readonly KeyValuePair AlphaCharacterRepresentation = new KeyValuePair('a', "[A-Za-z]"); private static readonly KeyValuePair NumericCharacterRepresentation = new KeyValuePair('9', "[0-9]"); private static readonly KeyValuePair AlphaNumericRepresentation = new KeyValuePair('*', "[A-Za-z0-9]"); @@ -56,28 +57,30 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions throw new ArgumentException("PlaceHolder can't be null or empty"); } - var escapeCharValue = textbox.GetValue(EscapeCharacterProperty) as string; - if (string.IsNullOrEmpty(escapeCharValue)) - { - throw new ArgumentException("EscapeCharacter can't be null or empty."); - } - - var escape = escapeCharValue[0]; - var escapedChars = new List(); - var builder = new StringBuilder(mask); - for (int i = 0; i < builder.Length - 1; i++) + + string escapedMask; + var shouldEscapeVariables = (bool)textbox.GetValue(EscapeVariablesProperty); + if (shouldEscapeVariables) { - if (builder[i] == escape) + var builder = new StringBuilder(mask); + for (int i = 0; i < builder.Length - 1; i++) { - escapedChars.Add(i); - builder.Remove(i, 1); + if (builder[i] == EscapeChar) + { + escapedChars.Add(i); + builder.Remove(i, 1); + } } + escapedMask = builder.ToString(); + } + else + { + escapedMask = mask; } - var escapedMask = builder.ToString(); - textbox.SetValue(MaskEscapedCharIndecesProperty, escapedChars); - textbox.SetValue(EscapeCharacterProperty, escapedMask); + textbox.SetValue(MaskEscapedCharactersProperty, escapedChars); + textbox.SetValue(EscapedMaskProperty, escapedMask); var placeHolder = placeHolderValue[0]; @@ -208,7 +211,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions } var escapedMask = textbox.GetValue(EscapedMaskProperty) as string; - var escapedChars = textbox.GetValue(MaskEscapedCharIndecesProperty) as List; + var escapedChars = textbox.GetValue(MaskEscapedCharactersProperty) as List; // to update the textbox text without triggering TextChanging text int oldSelectionStart = (int)textbox.GetValue(OldSelectionStartProperty); @@ -264,8 +267,8 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions private static void Textbox_TextChanging(TextBox textbox, TextBoxTextChangingEventArgs args) { var mask = textbox.GetValue(MaskProperty) as string; - var escapedMask = textbox.GetValue(EscapeCharacterProperty) as string; - var escapedChars = textbox.GetValue(MaskEscapedCharIndecesProperty) as List; + var escapedMask = textbox.GetValue(EscapedMaskProperty) as string; + var escapedChars = textbox.GetValue(MaskEscapedCharactersProperty) as List; var representationDictionary = textbox.GetValue(RepresentationDictionaryProperty) as Dictionary; var placeHolderValue = textbox?.GetValue(PlaceHolderProperty) as string;