Passing state of WPF ValidationRule to View Model in MVVM
Passing state of WPF ValidationRule to View Model in MVVM
I am stuck in a seemingly common requirement. I have a WPF Prism (for MVVM) application. My model implements the IDataErrorInfo for validation. The IDataErrorInfo works great for non-numeric properties. However, for numeric properties, if the user enters invalid characters (which are not numeric), then the data doesn't even reach the model because wpf cannot convert it to numeric type.
So, I had to use WPF ValidationRule to provide user some meaningful message for invalid numeric entries. All the buttons in the view are bound to DelegateCommand of prism (in view model) and the enabling/disabling of buttons is done in View Model itself.
Now if a wpf ValidationRule fail for some TextBox, how do I pass this information to View Model so that it can appropriately disable buttons in the view ?
Answer by stijn for Passing state of WPF ValidationRule to View Model in MVVM
If you provide a custom ValidationRule
implementation you can store the value it received, as well as storing the last result. PseudoCode:
public class IsInteger : ValidationRule { private int parsedValue; public IsInteger() { } public string LastValue{ get; private set; } public bool LastParseSuccesfull{ get; private set; } public int ParsedValue{ get{ return parsedValue; } } public override ValidationResult Validate( object value, CultureInfo cultureInfo ) { LastValue = (string) value; LastParseSuccesfull = Int32.TryParse( LastValue, cultureInfo, ref parsedValue ); return new ValidationResult( LastParseSuccesfull, LastParseSuccesfull ? "not a valid number" : null ); } }
Answer by Nix for Passing state of WPF ValidationRule to View Model in MVVM
Someone's solved it here (unfortunately its in VB) by creating a dependency property HasError in the VM which seems to be bound to the Validation.HasError. I don't completely understand it yet but it may help you:
http://wpfglue.wordpress.com/2009/12/03/forwarding-the-result-of-wpf-validation-in-mvvm/
Answer by Alex Blokh for Passing state of WPF ValidationRule to View Model in MVVM
Nirvan
The simplest way to solve this particular issue is to use a numeric textbox, which prevents the user from entering an invalid value (you can either do this via a Third Party Vendor, or find an open source solution, such as a class derived from Textbox that suppresses non numeric input).
The second way to handle this in MVVM without doing the above, is to define another field in you ViewModel which is a string, and bind that field to your textbox. Then, in the setter of your string field you can set the Integer, and assign a value to your numeric field:
Here is a rough example: (NOTE I did not test it, but it should give you the idea)
// original field private int _age; int Age { get { return _age; } set { _age = value; RaisePropertyChanged("Age"); } } private string _ageStr; string AgeStr { get { return _ageStr; } set { _ageStr = value; RaisePropertyChanged("AgeStr"); if (!String.IsNullOrEmpty(AgeStr) && IsNumeric(AgeStr) ) Age = intVal; } } private bool IsNumeric(string numStr) { int intVal; return int.TryParse(AgeStr, out intVal); } #region IDataErrorInfo Members public string this[string columnName] { get { if (columnName == "AgeStr" && !IsNumeric(AgeStr) return "Age must be numeric"; } }
0 comments:
Post a Comment