BindingOperations.ClearBinding(...) doesn't work when control is from a DataTemplate
.net, c#, data-binding, datatemplate, wpf
Solution
I found this MSDN post that covers the issue. While it seems to leave the issue without a real explanation, it does provide a workaround that seems to work just fine.
I replace the ClearBinding invocation with replacing the binding with some dummy value:
AssociatedObject.SetBinding(TextBox.TextProperty, "dummy");
Now it seems that this will break when the control is not from a `DataTemplate`. So to work-around that, and so cover `TextBox`es from `DataTemplate` and otherwise, I now do this:
BindingOperations.ClearBinding(AssociatedObject, TextBox.TextProperty);
if (BindingOperations.IsDataBound(AssociatedObject, TextBox.TextProperty))
AssociatedObject.SetBinding(TextBox.TextProperty, "dummy");
And voila, the binding is "severed" and so my watermark seems to work just fine now.
Problem
I'm a bit stuck on this one. Clearing the binding of the `TextProperty` of some `TextBox` that is not part of a `DataTemplate` works fine. But when the `TextBox` is part of a `DataTemplate`, clearing the binding seems to be a no-op as shown in the snip below. The watch value is `true` even after the binding is presumably cleared: Is this by design? If not, what am I doing wrong? Here is the MSDN documentation of `ClearBinding(...)`: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingoperations.clearbinding