How do I get a TextBox to only accept numeric input in WPF?

后端 未结 30 2751
悲哀的现实
悲哀的现实 2020-11-22 03:40

I\'m looking to accept digits and the decimal point, but no sign.

I\'ve looked at samples using the NumericUpDown control for Windows Forms, and this sample of a Num

30条回答
  •  余生分开走
    2020-11-22 04:10

    Here is a very simple and easy way to do this using MVVM.

    Bind your textBox with an integer property in the view model, and this will work like a gem ... it will even show validation when a non-integer is entered in the textbox.

    XAML code:

    
    

    View model code:

    private long _contactNo;
    public long contactNo
    {
        get { return _contactNo; }
        set
        {
            if (value == _contactNo)
                return;
            _contactNo = value;
            OnPropertyChanged();
        }
    }
    

提交回复
热议问题