We have a custom WPF control with multiple TextBox
. Each TextBox
has its Text
property bound to a dependency property on our custom control (using the generic.xaml
template).
We would like to alter the value entered in the TextBox
inside our custom control if the data provided is incorrect. We know we can use validation to accept or reject a value, but that would not meet our needs since we just want to slightly alter the value.
For example, if the user enters 11
in a particular TextBox
we want to update it to 2011
. Or we want to capitalize the value of one of the TextBox
regardless of what the user enters.
We cannot seem to find the proper WPF dependency property mechanism to perform this cleanly. We tried the CooerceCallback, but it will not update the TextBox
that initiated the change in the dependency property. Other controls that are bound to that dependency property will get the modified value, but not the caller.
One idea we had was to change the recommended default "property template" to do more than just SetValue(property, value)
. So an example would be:
public string Year
{ get { return (string)GetValue(YearProperty);}
{ set {
var newVal = UpdateValue(value);
SetValue(YearProperty, newVal);
}
}
But that does not work as binding with dependency properties will NOT use the underlying .Net property but use SetValue() directly.
If I have not confused everybody with this question, I would appreciate any help.
Regards,
Eric.
PS: We are using .Net 3.5 SP1