use a boolean property instead of a char property for backward-compatibility

This commit is contained in:
Muhammad Azeez 2019-02-22 22:43:20 +03:00
Родитель bd1c5449ff
Коммит 4e4a3d86cc
2 изменённых файлов: 31 добавлений и 28 удалений

Просмотреть файл

@ -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));
/// <summary>
/// Represents the character that's used to escape variables in the mask
/// Represents whether the variables in the mask can be escaped
/// </summary>
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<char, string>), 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<int>), typeof(TextBoxMask), new PropertyMetadata(null));
private static readonly DependencyProperty MaskEscapedCharactersProperty = DependencyProperty.RegisterAttached("MaskEscapedCharacters", typeof(List<int>), typeof(TextBoxMask), new PropertyMetadata(null));
/// <summary>
/// Gets mask value
@ -109,19 +109,19 @@ namespace Microsoft.Toolkit.Uwp.UI.Extensions
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string GetEscapeCharacter(DependencyObject obj)
public static bool GetEscapeVariables(DependencyObject obj)
{
return (string)obj.GetValue(EscapeCharacterProperty);
return (bool)obj.GetValue(EscapeVariablesProperty);
}
/// <summary>
/// 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
/// </summary>
/// <param name="obj"></param>
/// <param name="value"></param>
public static void SetEscapeCharacter(DependencyObject obj, string value)
public static void SetEscapeVariables(DependencyObject obj, bool value)
{
obj.SetValue(EscapeCharacterProperty, value);
obj.SetValue(EscapeVariablesProperty, value);
}
}
}

Просмотреть файл

@ -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<char, string> AlphaCharacterRepresentation = new KeyValuePair<char, string>('a', "[A-Za-z]");
private static readonly KeyValuePair<char, string> NumericCharacterRepresentation = new KeyValuePair<char, string>('9', "[0-9]");
private static readonly KeyValuePair<char, string> AlphaNumericRepresentation = new KeyValuePair<char, string>('*', "[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<int>();
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<int>;
var escapedChars = textbox.GetValue(MaskEscapedCharactersProperty) as List<int>;
// 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<int>;
var escapedMask = textbox.GetValue(EscapedMaskProperty) as string;
var escapedChars = textbox.GetValue(MaskEscapedCharactersProperty) as List<int>;
var representationDictionary = textbox.GetValue(RepresentationDictionaryProperty) as Dictionary<char, string>;
var placeHolderValue = textbox?.GetValue(PlaceHolderProperty) as string;