开发者

Create a custom TextBox where already contain a validation function

I'm using in my programa several times this code for my TextBox:

    private void ResultTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !IsTextAllowed(e.Text);
    }

    private static bool IsTextAllowed(string text)
    {
 开发者_高级运维       System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[^0-9.-]+"); //regex that matches disallowed text
        return !regex.IsMatch(text);
    }

I'd like to create a custom textbox where I don't have to write that code anymore in each user control. I have no idea to create custom controls in WPF.


public class MyTextBox : TextBox
{
     System.Text.RegularExpressions.Regex regex = newSystem.Text.RegularExpressions.Regex("[^0-9.-]+"); 
     protected override void OnPreviewTextInput(TextCompositionEventArgs e)
     {
        e.Handled = !regex.IsMatch(e.Text);
     }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜