0

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

Eric Liprandi
  • 5,324
  • 2
  • 49
  • 68
  • 1
    possible duplicate of [WPF Dependency Property Coercion binding issues](http://stackoverflow.com/questions/3003546/wpf-dependency-property-coercion-binding-issues) – CodeNaked Nov 02 '11 at 22:33
  • Thanks @CodeNaked, I must not have been searching with the right keywords as that answer never came up. But you are correct. We had already implemented the `UpdateTarget()` solution, but the behavior is fixed in .Net 4.0 – Eric Liprandi Nov 02 '11 at 23:03

1 Answers1

0

As pointed out by @CodeNaked, this is similar to Dependency Property Coercion binding issues and is Fixed in .Net 4.0. In the meantime, for .Net 3.5 SP1, we are forcing the update of the binding for the element that caused the change event (in our case, a TextBox). Hope this helps.

Regards,

Eric.

Community
  • 1
  • 1
Eric Liprandi
  • 5,324
  • 2
  • 49
  • 68