I know that CoerceValueCallback
is used to correct a value and that ValidateValueCallback
will return true or false. But my question is why we need ValidatevalueCallback
? We can simply use CoerceValueCallback
to validate (using if condition) and correct the value. Can you give some practical example of when to use coercion vs. validation?
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- how to Enumerate local fonts in silverlight 4
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
Value coercion is basically to change the value, if the the new value is not as system expected. A best example is Slider control. A Slider has both Minimum and Maximum properties. Clearly, it would be a problem if the Maximum value were allowed to fall below the Minimum value. Value coercion is used to prevent this invalid state from occuring.
Validate value, is something that system will only check whether the given input is valid or not. It will throw Argument Exception if value is invalid (if we returned false for such value). For example, we have Age property, and it should be in range of 0 to 120. In case the new value is 500, the system may warn the user instead coercing it to some hardcoded value.
Any way both callbacks are optional and can be used based on the requirement.
Here are the rules I follow for when to use coercion vs. validation.
Use
CoerceValueCallback
If...Use
ValidateValueCallback
If...So, it primarily depends on whether or not your property depends on other dependency properties or if you want others to be able to override your validation logic.
Since the
ValidateValueCallback
is not part of thePropertyMetadata
, inheritors cannot modify the callback through theDependencyProperty.OverrideMetadata
function.Also, since the
ValidateValueCallback
does not provide yourDependencyObject
as a parameter, you cannot perform advanced validation that depends on other dependency properties.Example 1
Suppose you have
Minimum
,Maximum
, &Value
properties. When any of these change, aCoerceValueCallback
shoud be used to ensure the other properties are consistent.That is,
Minmum
<=Value
<=Maximum
.However, assuming these values are doubles, then there are some values that would never make sense, namely
Double.NaN
,Double.PositiveInfinity
, andDouble.NegativeInfinity
. Therefore, aValidateValueCallback
should be used to verify that the double values are normal, numeric values.In fact, this is exactly how
RangeBase
works!Example 2
Suppose you have a
RegexTextBox
control which takes a string containing a regular expression (call itRegexString
). If a bad regular expression is provided, then what should be used instead? It might make sense to coerce it to be a null/empty value, rendering it useless; however, I suggest that this property be validated with aValidateValueCallback
. This is because any error is now thrown at compile-time when designing via the WPF designer.For this property, there shouldn't be a
CoerceValueCallback
at all.There is a whole lot of information describing how to use these callbacks. I'd suggest taking a look at the MSDN article, Dependency Property Callbacks and Validation, for more in-depth knowledge.