I have a datagrid bound to a collection of Parts. One of the properties of Part is Quantity, an integer, and one of the textboxes in the datagrid is bound to 开发者_如何学JAVAthis quantity field. When the user enters some garbage data into this box (any non-integer), the validation kicks in, the box is highlighted in red, and the row error message appears. When the datagrid is in that state, I want the "Save" button to be disabled, but I can't figure out any way to get this information. Validation.GetHasError in codebehind returns false, and the quantity fields display 0 in my viewmodel. How can I do this?
i use a wrapperproperty and IDataErrorInfo in my viewmodel for such cases. if you have a integer property and wpf cant convert the input to integer - you will get a BindingErrorException and it never hits the viewmodel.
public string MyQuantity {get ... set ...}
you can then easily use your CanSaveCommandExecute method to check the Error property of IDataErroInfo in your viewmodel. the advantage of this is that you have all information and control in your viewmodel.
精彩评论