Swapping data binding in code

Posted by Phil J Pearson on Stack Overflow See other posts from Stack Overflow or by Phil J Pearson
Published on 2010-03-25T12:01:02Z Indexed on 2010/03/25 12:03 UTC
Read the original article Hit count: 285

Filed under:
|

I have two data-bound text boxes. One is bound to a string and the other to a number. The 'default' binding is set in XAML. Under some circumstances I need to reverse the bindings at runtime (the string is usually a prefix but sometimes it's a suffix). I have the following code in my view model, called when the window is loaded:

Binding stringBinding = BindingOperations.GetBinding(view.seqLeft, TextBox.TextProperty);
Binding numberBinding = BindingOperations.GetBinding(view.seqRight, TextBox.TextProperty);
view.seqLeft.SetBinding(TextBlock.TextProperty, numberBinding);
view.seqRight.SetBinding(TextBlock.TextProperty, stringBinding);

After that the code loads the properties to which the binding refers.

The problem is that the 'new' binding doesn't seem to work. What have I missed? Is there a better way?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf