validation - Order of execution in a WPF Textbox -


I am running into a problem with a formatting converter and data validation. I have the following text box XML declaration

  & lt; Text box fontfilely = "segoyee" fontisystem = "16" fontweit = "medium" tabindex = "{binding tabbanking}" foreground = "black" opacity = "0.9" IsTabStop = "True" UID = "{Binding PriceID}" Text = "{Binding NewPrice, Converter = {StaticResource FormattingConverter}, Converter Parameter = '\ {0: C \}', mode = tw, UpdateSourceTrigger = LostFocus}" background = "#FFE6DED3" BorderBrush = "#FFE6DED3" datacentext = "{binding} String formatting = \ {0: c}}, NotifyOnValidationError = True} "padding =" 0 "key =" TextBox_KeyDown "accepts return =" False "& g T; & Lt; / Text box & gt;  

The issue I'm on is a data validation problem. When a user enters an invalid value (example: "ABC" or "Value of 0.0.4"), then the textbox tries to convert to the "formattingconverter" method. (Convertback method pasted below) This causes exceptions and program errors. Is there a way to delay the formatting converter call or bypass it, if the data in the text box is not valid?

  Public Object Convertback (Object Value, SystemTip Target Type, Object Parameter, System Globilization CultureEfo Culture) {var objTypeConverter = System.ComponentModel.TypeDescriptor.GetConverter (targetType); Object objReturnValue = faucet; If (objTypeConverter.CanConvertFrom (value.GetType ())) {objReturnValue = objTypeConverter.ConvertFrom (value.ToString (). Change ("$", "")); } Return objReturnValue; }  

Thank you,

Convertback of a converter will always send data to Convert the appropriate value to the target object It is the responsibility of the converter to handle exceptions (and in case of an exception, return the original value, so that the bond structure will also know that it is an invalid value).

  public commodity convertback value, system Type target type, object parameter, system Globalization. Culturfo culture) {var objTypeConverter = System.ComponentModel.TypeDescriptor.GetConverter (targetType); // The default return value is the original value - if the conversion fails, consider this value back / 0. so that the binding framework will see an invalid value (and not just blank //). Object objReturnValue = value; If (objTypeConverter.CanConvertFrom (value.GetType ())) {try {objReturnValue = objTypeConverter.ConvertFrom (value.ToString (). Change ("$", "")); } FormatException {} // Hold your potential exceptions and return the original value to ignore them} return objReturnValue; }  

You can also return DependencyProperty.UnsetValue, although in practice I prefer to see the actual error message returned in an invalid value, rather than returning an unset value.


Comments