Add a preview text input event. Like so: <TextBox PreviewTextInput="PreviewTextInput" />
.
Then inside that set the e.Handled if the text isn't allowed. e.Handled = !IsTextAllowed(e.Text);
I use a simple regex in IsTextAllowed to see if I should allow what they've typed. In my case I only want to allow numbers, dots and dashes.
private static bool IsTextAllowed(string text)
{
Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
return !regex.IsMatch(text);
}
If you want to prevent pasting of incorrect data hook up the DataObject.Pasting eventDataObject.Pasting="TextBoxPasting"
as shown here(Masking input to a WPF TextBox 참조)
'App' 카테고리의 다른 글
Compare hash bytes of two files (0) | 2011.06.09 |
---|---|
how-to-create-thumbnail-of-video-using-c (0) | 2010.06.11 |
C# Sample Codes and Examples .. using media handler pro (0) | 2010.06.01 |
How to detect No mouse movement (0) | 2010.02.22 |
Linq to SQL Database Connection String Issue (0) | 2010.02.17 |