WPF How to create a Custom Textbox with validation and binding

前端 未结 4 1132
死守一世寂寞
死守一世寂寞 2021-01-15 05:47

I\'m developing a custom text box for currency editing.
I\'ve seen some ready to use ones, but they\'re complicated and/or not really usable, forcing you to bad practi

4条回答
  •  清歌不尽
    2021-01-15 05:59

    take look on this article i think it will help you. http://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation

    or you can put this

    private static bool IsTextAllowed(string text)
    {
        Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
        return !regex.IsMatch(text);
    }
    

    and in PreviewTextInput event put this

    e.Handled = !IsTextAllowed(e.Text);

提交回复
热议问题