Home > Net >  Ask a WPF, MvvmLight data binding problem
Ask a WPF, MvvmLight data binding problem

Time:09-26

 
Public class TestModel: ViewModelBase
{
Private int _Height;
///& lt; Summary>
///
///& lt;/summary>
Public int Height {get {return _Height; } the set {_Height=value; RaisePropertyChanged (" Height "); }}
}


I define a model for data binding, however, when user input may input of type string, the database of int is Height? Type, allowing is empty, when submitted to save under the Mvvm pattern, if the user to enter the Chinese, although text input box will turn red, but, will also perform the back of the data code, because ViewModelBase detect errors, forced into a Height=0, continue to perform database code, Height=0 saved into the database, the data for the user to enter the Chinese, but there is no hint, program execution is successful, the database holds the Height=0, how to capture the input error, prompt, does not perform data save operation?

CodePudding user response:

WPF handle input validation method is more, can use IDataErrorInfo MVVM mode, which is again on the Model, the interface
Others are ValidationRule validation (large projects is not recommended, to write a heap of ValidationRule, also want to write a bunch of binding on Xaml), also can be realized in custom controls the internal input validation;

CodePudding user response:

If the user to enter the Chinese, although text input box will become red
- "using regular expressions in PreviewTextInput or KeyDown event has occurred and set the Handle value judgment entered,
  •  Tags:  
  • C#
  • Related