Swapping data binding at runtime [solved]
-
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 tried 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. [edit] The problem is that it doesn't work because
GetBinding
returns a reference to the existing binding. I would need to create a copy but that seems as if it would be loads of hassle. What have I missed? Is there a better way? The problem is just thatSetBinding
does nothing! If youClearBinding
and thenGetBinding
(as a check) you get the original binding back. If you make an entirely new binding andSetBinding
(with or withoutClearBinding
first) you STILL have the original binding. There are no errors or exceptions -SetBinding
is simply ignored! WHAT'S GOING ON?? :mad: :sigh: [/edit]Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
modified on Thursday, March 25, 2010 1:04 PM
-
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 tried 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. [edit] The problem is that it doesn't work because
GetBinding
returns a reference to the existing binding. I would need to create a copy but that seems as if it would be loads of hassle. What have I missed? Is there a better way? The problem is just thatSetBinding
does nothing! If youClearBinding
and thenGetBinding
(as a check) you get the original binding back. If you make an entirely new binding andSetBinding
(with or withoutClearBinding
first) you STILL have the original binding. There are no errors or exceptions -SetBinding
is simply ignored! WHAT'S GOING ON?? :mad: :sigh: [/edit]Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
modified on Thursday, March 25, 2010 1:04 PM
-
Hi Have you tried to clear the binding first and then create new bindings in code? I think that should work. Uros
See my edit to my original post.
Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
-
See my edit to my original post.
Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
-
I tried in simple example and it works fine. What are the variables view.seqLeft and view.seqRight? They should be the TextBox-es and not the properties in your view model to wich to TextBox-es are bind to
They are TextBox elements.
Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
-
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 tried 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. [edit] The problem is that it doesn't work because
GetBinding
returns a reference to the existing binding. I would need to create a copy but that seems as if it would be loads of hassle. What have I missed? Is there a better way? The problem is just thatSetBinding
does nothing! If youClearBinding
and thenGetBinding
(as a check) you get the original binding back. If you make an entirely new binding andSetBinding
(with or withoutClearBinding
first) you STILL have the original binding. There are no errors or exceptions -SetBinding
is simply ignored! WHAT'S GOING ON?? :mad: :sigh: [/edit]Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
modified on Thursday, March 25, 2010 1:04 PM
The only thing wrong with my code was the
TextBlock.TextProperty
in theSetBinding
calls! They should, of course, have beenTextBox.TextProperty
but I'd messed with it so long I wasn't seeing the wood for the trees.Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.