TextBox 입력 제한을 정의하는 방법은 무엇입니까?
TextBox가 대문자 또는 예를 들어 숫자 만 허용하거나 특수 문자를 넣지 못하도록 제한하려면 어떻게해야합니까?
물론 TextInput 이벤트를 포착하고 여기에서 텍스트를 처리하는 것은 케이크 조각이지만이 작업을 수행하는 적절한 방법입니까?
이전에 다음과 같이 사용할 수있는 연결된 동작으로이 작업을 수행했습니다.
<TextBox b:Masking.Mask="^\p{Lu}*$"/>
첨부 된 동작 코드는 다음과 같습니다.
/// <summary>
/// Provides masking behavior for any <see cref="TextBox"/>.
/// </summary>
public static class Masking
{
private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly("MaskExpression",
typeof(Regex),
typeof(Masking),
new FrameworkPropertyMetadata());
/// <summary>
/// Identifies the <see cref="Mask"/> dependency property.
/// </summary>
public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask",
typeof(string),
typeof(Masking),
new FrameworkPropertyMetadata(OnMaskChanged));
/// <summary>
/// Identifies the <see cref="MaskExpression"/> dependency property.
/// </summary>
public static readonly DependencyProperty MaskExpressionProperty = _maskExpressionPropertyKey.DependencyProperty;
/// <summary>
/// Gets the mask for a given <see cref="TextBox"/>.
/// </summary>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask is to be retrieved.
/// </param>
/// <returns>
/// The mask, or <see langword="null"/> if no mask has been set.
/// </returns>
public static string GetMask(TextBox textBox)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
return textBox.GetValue(MaskProperty) as string;
}
/// <summary>
/// Sets the mask for a given <see cref="TextBox"/>.
/// </summary>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask is to be set.
/// </param>
/// <param name="mask">
/// The mask to set, or <see langword="null"/> to remove any existing mask from <paramref name="textBox"/>.
/// </param>
public static void SetMask(TextBox textBox, string mask)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
textBox.SetValue(MaskProperty, mask);
}
/// <summary>
/// Gets the mask expression for the <see cref="TextBox"/>.
/// </summary>
/// <remarks>
/// This method can be used to retrieve the actual <see cref="Regex"/> instance created as a result of setting the mask on a <see cref="TextBox"/>.
/// </remarks>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask expression is to be retrieved.
/// </param>
/// <returns>
/// The mask expression as an instance of <see cref="Regex"/>, or <see langword="null"/> if no mask has been applied to <paramref name="textBox"/>.
/// </returns>
public static Regex GetMaskExpression(TextBox textBox)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
return textBox.GetValue(MaskExpressionProperty) as Regex;
}
private static void SetMaskExpression(TextBox textBox, Regex regex)
{
textBox.SetValue(_maskExpressionPropertyKey, regex);
}
private static void OnMaskChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var textBox = dependencyObject as TextBox;
var mask = e.NewValue as string;
textBox.PreviewTextInput -= textBox_PreviewTextInput;
textBox.PreviewKeyDown -= textBox_PreviewKeyDown;
DataObject.RemovePastingHandler(textBox, Pasting);
if (mask == null)
{
textBox.ClearValue(MaskProperty);
textBox.ClearValue(MaskExpressionProperty);
}
else
{
textBox.SetValue(MaskProperty, mask);
SetMaskExpression(textBox, new Regex(mask, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace));
textBox.PreviewTextInput += textBox_PreviewTextInput;
textBox.PreviewKeyDown += textBox_PreviewKeyDown;
DataObject.AddPastingHandler(textBox, Pasting);
}
}
private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
if (maskExpression == null)
{
return;
}
var proposedText = GetProposedText(textBox, e.Text);
if (!maskExpression.IsMatch(proposedText))
{
e.Handled = true;
}
}
private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
if (maskExpression == null)
{
return;
}
//pressing space doesn't raise PreviewTextInput - no idea why, but we need to handle
//explicitly here
if (e.Key == Key.Space)
{
var proposedText = GetProposedText(textBox, " ");
if (!maskExpression.IsMatch(proposedText))
{
e.Handled = true;
}
}
}
private static void Pasting(object sender, DataObjectPastingEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
if (maskExpression == null)
{
return;
}
if (e.DataObject.GetDataPresent(typeof(string)))
{
var pastedText = e.DataObject.GetData(typeof(string)) as string;
var proposedText = GetProposedText(textBox, pastedText);
if (!maskExpression.IsMatch(proposedText))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
private static string GetProposedText(TextBox textBox, string newText)
{
var text = textBox.Text;
if (textBox.SelectionStart != -1)
{
text = text.Remove(textBox.SelectionStart, textBox.SelectionLength);
}
text = text.Insert(textBox.CaretIndex, newText);
return text;
}
}
이전에 패턴을 위반할 수 있었던 다음 작업도 처리하여 Kent Boogaart의 답변을 개선했습니다.
- 역행 키이
- 패턴을 위반할 수있는 방식으로 텍스트 선택 및 드래그
- 잘라 내기 명령
예를 들어 Kent Boogaart의 답변은 사용자가 먼저 "abc"를 입력하여 "ac"를 입력하고 그 후에 다음 정규식을 위반하는 백 스페이스로 "b"를 삭제하도록 허용했습니다.
^(a|ab|abc)$
사용법 (변경되지 않음) :
<TextBox b:Masking.Mask="^\p{Lu}*$"/>
마스크 클래스 :
public static class Masking
{
private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly("MaskExpression",
typeof(Regex),
typeof(Masking),
new FrameworkPropertyMetadata());
/// <summary>
/// Identifies the <see cref="Mask"/> dependency property.
/// </summary>
public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask",
typeof(string),
typeof(Masking),
new FrameworkPropertyMetadata(OnMaskChanged));
/// <summary>
/// Identifies the <see cref="MaskExpression"/> dependency property.
/// </summary>
public static readonly DependencyProperty MaskExpressionProperty = _maskExpressionPropertyKey.DependencyProperty;
/// <summary>
/// Gets the mask for a given <see cref="TextBox"/>.
/// </summary>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask is to be retrieved.
/// </param>
/// <returns>
/// The mask, or <see langword="null"/> if no mask has been set.
/// </returns>
public static string GetMask(TextBox textBox)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
return textBox.GetValue(MaskProperty) as string;
}
/// <summary>
/// Sets the mask for a given <see cref="TextBox"/>.
/// </summary>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask is to be set.
/// </param>
/// <param name="mask">
/// The mask to set, or <see langword="null"/> to remove any existing mask from <paramref name="textBox"/>.
/// </param>
public static void SetMask(TextBox textBox, string mask)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
textBox.SetValue(MaskProperty, mask);
}
/// <summary>
/// Gets the mask expression for the <see cref="TextBox"/>.
/// </summary>
/// <remarks>
/// This method can be used to retrieve the actual <see cref="Regex"/> instance created as a result of setting the mask on a <see cref="TextBox"/>.
/// </remarks>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask expression is to be retrieved.
/// </param>
/// <returns>
/// The mask expression as an instance of <see cref="Regex"/>, or <see langword="null"/> if no mask has been applied to <paramref name="textBox"/>.
/// </returns>
public static Regex GetMaskExpression(TextBox textBox)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
return textBox.GetValue(MaskExpressionProperty) as Regex;
}
private static void SetMaskExpression(TextBox textBox, Regex regex)
{
textBox.SetValue(_maskExpressionPropertyKey, regex);
}
private static void OnMaskChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var textBox = dependencyObject as TextBox;
var mask = e.NewValue as string;
textBox.PreviewTextInput -= textBox_PreviewTextInput;
textBox.PreviewKeyDown -= textBox_PreviewKeyDown;
DataObject.RemovePastingHandler(textBox, Pasting);
DataObject.RemoveCopyingHandler(textBox, NoDragCopy);
CommandManager.RemovePreviewExecutedHandler(textBox, NoCutting);
if (mask == null)
{
textBox.ClearValue(MaskProperty);
textBox.ClearValue(MaskExpressionProperty);
}
else
{
textBox.SetValue(MaskProperty, mask);
SetMaskExpression(textBox, new Regex(mask, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace));
textBox.PreviewTextInput += textBox_PreviewTextInput;
textBox.PreviewKeyDown += textBox_PreviewKeyDown;
DataObject.AddPastingHandler(textBox, Pasting);
DataObject.AddCopyingHandler(textBox, NoDragCopy);
CommandManager.AddPreviewExecutedHandler(textBox, NoCutting);
}
}
private static void NoCutting(object sender, ExecutedRoutedEventArgs e)
{
if(e.Command == ApplicationCommands.Cut)
{
e.Handled = true;
}
}
private static void NoDragCopy(object sender, DataObjectCopyingEventArgs e)
{
if (e.IsDragDrop)
{
e.CancelCommand();
}
}
private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
if (maskExpression == null)
{
return;
}
var proposedText = GetProposedText(textBox, e.Text);
if (!maskExpression.IsMatch(proposedText))
{
e.Handled = true;
}
}
private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
if (maskExpression == null)
{
return;
}
string proposedText = null;
//pressing space doesn't raise PreviewTextInput, reasons here http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/446ec083-04c8-43f2-89dc-1e2521a31f6b?prof=required
if (e.Key == Key.Space)
{
proposedText = GetProposedText(textBox, " ");
}
// Same story with backspace
else if(e.Key == Key.Back)
{
proposedText = GetProposedTextBackspace(textBox);
}
if (proposedText != null && proposedText != string.Empty && !maskExpression.IsMatch(proposedText))
{
e.Handled = true;
}
}
private static void Pasting(object sender, DataObjectPastingEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
if (maskExpression == null)
{
return;
}
if (e.DataObject.GetDataPresent(typeof(string)))
{
var pastedText = e.DataObject.GetData(typeof(string)) as string;
var proposedText = GetProposedText(textBox, pastedText);
if (!maskExpression.IsMatch(proposedText))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
private static string GetProposedTextBackspace(TextBox textBox)
{
var text = GetTextWithSelectionRemoved(textBox);
if (textBox.SelectionStart > 0 && textBox.SelectionLength == 0)
{
text = text.Remove(textBox.SelectionStart-1, 1);
}
return text;
}
private static string GetProposedText(TextBox textBox, string newText)
{
var text = GetTextWithSelectionRemoved(textBox);
text = text.Insert(textBox.CaretIndex, newText);
return text;
}
private static string GetTextWithSelectionRemoved(TextBox textBox)
{
var text = textBox.Text;
if (textBox.SelectionStart != -1)
{
text = text.Remove(textBox.SelectionStart, textBox.SelectionLength);
}
return text;
}
}
컬러 테마를 지원하도록 VitalyB의 코드를 변경했습니다. RegEx 스크립트를 충족하지 않는 경우 사용자 입력을 차단하는 대신 텍스트 상자 만 강조 표시합니다. 텍스트 상자는 상호 작용이없는 테마 기본값이되며 입력이 설정된 후 값에 따라 기본적으로 밝은 녹색 또는 빨간색으로 설정됩니다. 다음을 사용하여 프로그래밍 방식으로 실패 및 통과 색상을 설정할 수도 있습니다.
b:ColorMasking.PassColor = "Hexadecimal Value"
b:ColorMasking.FailColor = "Hexadecimal Value"
수업은 다음과 같습니다.
public class ColorMasking : DependencyObject
{
private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly("MaskExpression",
typeof(Regex),
typeof(ColorMasking),
new FrameworkPropertyMetadata());
/// <summary>
/// Identifies the <see cref="Mask"/> dependency property.
/// </summary>
///
public static readonly DependencyProperty PassColorProperty = DependencyProperty.RegisterAttached("PassColor",
typeof(string),
typeof(ColorMasking),
new PropertyMetadata("#99FF99"));
public static void SetPassColor(DependencyObject obj, string passColor)
{
obj.SetValue(PassColorProperty, passColor);
}
public static string GetPassColor(DependencyObject obj)
{
return (string)obj.GetValue(PassColorProperty);
}
public static readonly DependencyProperty FailColorProperty = DependencyProperty.RegisterAttached("FailColor",
typeof(string),
typeof(ColorMasking),
new PropertyMetadata("#FFCCFF"));
public static void SetFailColor(DependencyObject obj, string failColor)
{
obj.SetValue(FailColorProperty, failColor);
}
public static string GetFailColor(DependencyObject obj)
{
return (string)obj.GetValue(FailColorProperty);
}
public static readonly DependencyProperty MaskProperty = DependencyProperty.RegisterAttached("Mask",
typeof(string),
typeof(ColorMasking),
new FrameworkPropertyMetadata(OnMaskChanged));
private static void OnPassColorChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var textBox = dependencyObject as TextBox;
var color = e.NewValue as string;
textBox.SetValue(PassColorProperty, color);
}
/// <summary>
/// Identifies the <see cref="MaskExpression"/> dependency property.
/// </summary>
public static readonly DependencyProperty MaskExpressionProperty = _maskExpressionPropertyKey.DependencyProperty;
/// <summary>
/// Gets the mask for a given <see cref="TextBox"/>.
/// </summary>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask is to be retrieved.
/// </param>
/// <returns>
/// The mask, or <see langword="null"/> if no mask has been set.
/// </returns>
public static string GetMask(TextBox textBox)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
return textBox.GetValue(MaskProperty) as string;
}
/// <summary>
/// Sets the mask for a given <see cref="TextBox"/>.
/// </summary>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask is to be set.
/// </param>
/// <param name="mask">
/// The mask to set, or <see langword="null"/> to remove any existing mask from <paramref name="textBox"/>.
/// </param>
public static void SetMask(TextBox textBox, string mask)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
textBox.SetValue(MaskProperty, mask);
}
/// <summary>
/// Gets the mask expression for the <see cref="TextBox"/>.
/// </summary>
/// <remarks>
/// This method can be used to retrieve the actual <see cref="Regex"/> instance created as a result of setting the mask on a <see cref="TextBox"/>.
/// </remarks>
/// <param name="textBox">
/// The <see cref="TextBox"/> whose mask expression is to be retrieved.
/// </param>
/// <returns>
/// The mask expression as an instance of <see cref="Regex"/>, or <see langword="null"/> if no mask has been applied to <paramref name="textBox"/>.
/// </returns>
public static Regex GetMaskExpression(TextBox textBox)
{
if (textBox == null)
{
throw new ArgumentNullException("textBox");
}
return textBox.GetValue(MaskExpressionProperty) as Regex;
}
private static void SetMaskExpression(TextBox textBox, Regex regex)
{
textBox.SetValue(_maskExpressionPropertyKey, regex);
}
private static void OnMaskChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var textBox = dependencyObject as TextBox;
var mask = e.NewValue as string;
textBox.PreviewTextInput -= textBox_PreviewTextInput;
textBox.PreviewKeyDown -= textBox_PreviewKeyDown;
DataObject.RemovePastingHandler(textBox, Pasting);
DataObject.RemoveCopyingHandler(textBox, NoDragCopy);
CommandManager.RemovePreviewExecutedHandler(textBox, NoCutting);
if (mask == null)
{
textBox.ClearValue(MaskProperty);
textBox.ClearValue(MaskExpressionProperty);
}
else
{
textBox.SetValue(MaskProperty, mask);
SetMaskExpression(textBox, new Regex(mask, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace));
textBox.PreviewTextInput += textBox_PreviewTextInput;
textBox.PreviewKeyDown += textBox_PreviewKeyDown;
DataObject.AddPastingHandler(textBox, Pasting);
DataObject.AddCopyingHandler(textBox, NoDragCopy);
CommandManager.AddPreviewExecutedHandler(textBox, NoCutting);
}
}
private static void NoCutting(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == ApplicationCommands.Cut)
{
e.Handled = true;
}
}
private static void NoDragCopy(object sender, DataObjectCopyingEventArgs e)
{
if (e.IsDragDrop)
{
e.CancelCommand();
}
}
private static void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
string passHex = (string)textBox.GetValue(PassColorProperty);
string failHex = (string)textBox.GetValue(FailColorProperty);
Color passColor = Extensions.ToColorFromHex(passHex);
Color failColor = Extensions.ToColorFromHex(failHex);
if (maskExpression == null)
{
return;
}
var proposedText = GetProposedText(textBox, e.Text);
if (!maskExpression.IsMatch(proposedText))
{
textBox.Background = new SolidColorBrush(failColor);
}
else
{
textBox.Background = new SolidColorBrush(passColor);
}
}
private static void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
var textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
string passHex = (string)textBox.GetValue(PassColorProperty);
string failHex = (string)textBox.GetValue(FailColorProperty);
Color passColor = Extensions.ToColorFromHex(passHex);
Color failColor = Extensions.ToColorFromHex(failHex);
if (maskExpression == null)
{
return;
}
string proposedText = null;
//pressing space doesn't raise PreviewTextInput, reasons here http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/446ec083-04c8-43f2-89dc-1e2521a31f6b?prof=required
if (e.Key == Key.Space)
{
proposedText = GetProposedText(textBox, " ");
}
// Same story with backspace
else if (e.Key == Key.Back)
{
proposedText = GetProposedTextBackspace(textBox);
}
if (proposedText != null && !maskExpression.IsMatch(proposedText))
{
textBox.Background = new SolidColorBrush(failColor);
}
else
{
textBox.Background = new SolidColorBrush(passColor);
}
}
private static void Pasting(object sender, DataObjectPastingEventArgs e)
{
TextBox textBox = sender as TextBox;
var maskExpression = GetMaskExpression(textBox);
string passHex = (string)textBox.GetValue(PassColorProperty);
string failHex = (string)textBox.GetValue(FailColorProperty);
Color passColor = Extensions.ToColorFromHex(passHex);
Color failColor = Extensions.ToColorFromHex(failHex);
if (maskExpression == null)
{
return;
}
if (e.DataObject.GetDataPresent(typeof(string)))
{
var pastedText = e.DataObject.GetData(typeof(string)) as string;
var proposedText = GetProposedText(textBox, pastedText);
if (!maskExpression.IsMatch(proposedText))
{
textBox.Background = new SolidColorBrush(failColor);
}
else
{
textBox.Background = new SolidColorBrush(passColor);
}
}
else
{
textBox.Background = new SolidColorBrush(failColor);
}
}
private static string GetProposedTextBackspace(TextBox textBox)
{
var text = GetTextWithSelectionRemoved(textBox);
if (textBox.SelectionStart > 0)
{
text = text.Remove(textBox.SelectionStart - 1, 1);
}
return text;
}
private static string GetProposedText(TextBox textBox, string newText)
{
var text = GetTextWithSelectionRemoved(textBox);
text = text.Insert(textBox.CaretIndex, newText);
return text;
}
private static string GetTextWithSelectionRemoved(TextBox textBox)
{
var text = textBox.Text;
if (textBox.SelectionStart != -1)
{
text = text.Remove(textBox.SelectionStart, textBox.SelectionLength);
}
return text;
}
}
스크립트를 실행하려면 Aaron C가 작성한 클래스가 필요합니다. Silverlight / WPF는 여기에 표시된 16 진수 색상으로 타원을 설정합니다 . http://www.wiredprairie.us/blog/index.php/archives/659
웹 사이트가 이동 된 경우 코드는 다음과 같습니다.
public static class Extensions
{
public static void SetFromHex(this Color c, string hex)
{
Color c1 = ToColorFromHex(hex);
c.A = c1.A;
c.R = c1.R;
c.G = c1.G;
c.B = c1.B;
}
public static Color ToColorFromHex(string hex)
{
if (string.IsNullOrEmpty(hex))
{
throw new ArgumentNullException("hex");
}
// remove any "#" characters
while (hex.StartsWith("#"))
{
hex = hex.Substring(1);
}
int num = 0;
// get the number out of the string
if (!Int32.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out num))
{
throw new ArgumentException("Color not in a recognized Hex format.");
}
int[] pieces = new int[4];
if (hex.Length > 7)
{
pieces[0] = ((num >> 24) & 0x000000ff);
pieces[1] = ((num >> 16) & 0x000000ff);
pieces[2] = ((num >> 8) & 0x000000ff);
pieces[3] = (num & 0x000000ff);
}
else if (hex.Length > 5)
{
pieces[0] = 255;
pieces[1] = ((num >> 16) & 0x000000ff);
pieces[2] = ((num >> 8) & 0x000000ff);
pieces[3] = (num & 0x000000ff);
}
else if (hex.Length == 3)
{
pieces[0] = 255;
pieces[1] = ((num >> 8) & 0x0000000f);
pieces[1] += pieces[1] * 16;
pieces[2] = ((num >> 4) & 0x000000f);
pieces[2] += pieces[2] * 16;
pieces[3] = (num & 0x000000f);
pieces[3] += pieces[3] * 16;
}
return Color.FromArgb((byte)pieces[0], (byte)pieces[1], (byte)pieces[2], (byte)pieces[3]);
}
}
private void TextBox1_SelectionChanged(object sender, RoutedEventArgs e)
{
string txt = TextBox1.Text;
if (txt != "")
{
TextBox1.Text = Regex.Replace(TextBox1.Text, "[^0-9]", "");
if (txt != TextBox1.Text)
{
TextBox1.Select(TextBox1.Text.Length, 0);
}
}
}
여기에 기존 솔루션의 또 다른 버전이 있습니다. 변경을 취소하기위한 이전 텍스트, 새 텍스트 및 플래그를 제공하는 "TextChanging"이벤트를 시뮬레이션하는 동작입니다. 이렇게하면 원하는 필터를 구현할 수 있습니다.
공백 문자에만 사용되는 "PreviewKeyDown"처리기를 제거했습니다. TextBox는 라우팅 된 명령으로 모든 것을 관리하는 것처럼 보이며 "Space"에는 자체 명령이 있지만 공개되지는 않습니다. 또한 "Ctrl + Backspace"(이전 단어 삭제)와 같은 추가 키보드 단축키에 대한 지원을 추가했습니다.
public class TextChangingBehavior : Behavior<TextBox>
{
public event EventHandler<TextChangingEventArgs> TextChanging;
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewTextInput += OnPreviewTextInput;
CommandManager.AddPreviewExecutedHandler(AssociatedObject, OnPreviewExecutedHandler);
DataObject.AddCopyingHandler(AssociatedObject, OnCopying);
DataObject.AddPastingHandler(AssociatedObject, OnPasting);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PreviewTextInput -= OnPreviewTextInput;
CommandManager.RemovePreviewExecutedHandler(AssociatedObject, OnPreviewExecutedHandler);
DataObject.RemoveCopyingHandler(AssociatedObject, OnCopying);
DataObject.RemovePastingHandler(AssociatedObject, OnPasting);
}
#region Text
private enum CharCategory
{
LetterOrDigit,
Whitespace,
Other
}
private CharCategory GetCharCategory(char c)
{
if (char.IsLetterOrDigit(c))
return CharCategory.LetterOrDigit;
else if (char.IsWhiteSpace(c))
return CharCategory.Whitespace;
else
return CharCategory.Other;
}
private string GetText(string input = null)
{
var box = AssociatedObject;
var text = box.Text ?? string.Empty;
if (input != null)
{
// Delete selection
var deleteCount = box.SelectionLength;
if (deleteCount > 0)
text = text.Remove(box.SelectionStart, deleteCount);
// Insert input
if (input.Length > 0)
text = text.Insert(box.CaretIndex, input);
}
return text;
}
#endregion
private void OnPreviewExecutedHandler(object sender, ExecutedRoutedEventArgs e)
{
var box = AssociatedObject;
var selectionExists = box.SelectionLength > 0;
var caretIndex = box.CaretIndex;
string newText = null;
if (e.Command == ApplicationCommands.Cut)
{
if (selectionExists)
newText = GetText(string.Empty);
}
else if (e.Command == EditingCommands.Backspace)
{
if (selectionExists)
newText = GetText(string.Empty);
else if (caretIndex > 0)
newText = GetText().Remove(caretIndex - 1, 1);
}
else if (e.Command == EditingCommands.Delete)
{
if (selectionExists)
newText = GetText(string.Empty);
else
{
newText = GetText();
if (caretIndex >= newText.Length)
newText = null;
else
newText = newText.Remove(caretIndex, 1);
}
}
else if (e.Command == EditingCommands.DeletePreviousWord)
{
if (selectionExists)
newText = GetText(string.Empty);
else if (caretIndex > 0)
{
newText = GetText();
var startIndex = caretIndex;
// Include whitespaces
do
startIndex--;
while (startIndex > 0 && char.IsWhiteSpace(newText[startIndex]));
// Include the next block
var currentCategory = GetCharCategory(newText[startIndex]);
while (startIndex > 0 && GetCharCategory(newText[startIndex - 1]) == currentCategory)
startIndex--;
newText = newText.Remove(startIndex, caretIndex - startIndex);
}
}
else if (e.Command == EditingCommands.DeleteNextWord)
{
if (selectionExists)
newText = GetText(string.Empty);
else
{
newText = GetText();
if (caretIndex >= newText.Length)
newText = null;
else
{
var endIndex = caretIndex + 1;
// Include the current block
var currentCategory = GetCharCategory(newText[caretIndex]);
while (endIndex < newText.Length && GetCharCategory(newText[endIndex]) == currentCategory)
endIndex++;
// Include whitespaces
while (endIndex < newText.Length && char.IsWhiteSpace(newText[endIndex]))
endIndex++;
newText = newText.Remove(caretIndex, endIndex - caretIndex);
}
}
}
else if (e.Command is RoutedUICommand cmd && new[] { "Space", "ShiftSpace" }.Contains(cmd.Name))
{
newText = GetText(" ");
}
if (newText != null && OnProcessChange(newText))
e.Handled = true;
}
private void OnCopying(object sender, DataObjectCopyingEventArgs e)
{
if (e.IsDragDrop)
{
if (OnProcessChange(GetText(string.Empty)))
e.CancelCommand();
}
}
private void OnPasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(string)))
{
if (OnProcessChange(GetText((string)e.DataObject.GetData(typeof(string)))))
e.CancelCommand();
}
}
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!string.IsNullOrEmpty(e.Text))
{
if (OnProcessChange(GetText(e.Text)))
e.Handled = true;
}
}
private bool OnProcessChange(string newValue)
{
var oldValue = GetText();
if (string.Equals(oldValue, newValue, StringComparison.Ordinal))
return false;
else
{
var args = new TextChangingEventArgs(oldValue, newValue);
OnTextChanging(args);
return args.Cancel;
}
}
protected virtual void OnTextChanging(TextChangingEventArgs e)
{
TextChanging?.Invoke(this, e);
}
}
public class TextChangingEventArgs : EventArgs
{
public string OldValue { get; }
public string NewValue { get; }
public bool Cancel { get; set; }
public TextChangingEventArgs(string oldValue, string newValue)
{
OldValue = oldValue;
NewValue = newValue;
}
}
또 다른 가능한 해결책은 Winforms의 "MaskedTextBox"에서 사용하는 "MaskedTextProvider"클래스를 사용하여 "Masked TextBox"wpf 구현 중 하나를 사용하는 것입니다.
Two potential solutions are found here -> https://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox and here -> http://marlongrech.wordpress.com/2007/10/28/masked-textbox/
ReferenceURL : https://stackoverflow.com/questions/1103765/how-to-define-textbox-input-restrictions
'developer tip' 카테고리의 다른 글
OS의 제한을 초과하여 키 반복률을 높이려면 어떻게해야합니까? (0) | 2021.01.09 |
---|---|
단일 문자를 정수로 변환하는 방법 (0) | 2021.01.09 |
주어진 날짜 이후 첫 번째 월요일의 날짜 찾기 (0) | 2021.01.09 |
Doctrine2 쿼리에서 제한 및 오프셋 사용 (0) | 2021.01.09 |
Kotlin에서 예상되는 예외 테스트 (0) | 2021.01.09 |