Ivan Crojach Karačić
Ivan Crojach Karačić

Reputation: 1911

Binding vs Explicit Assignment of a Variable

I am thinking about the following issue.

If I have for example a textbox/slider/combobox which value is bound to something like

<TextBox Name=textBox Text="{Binding Text}"/>

and then do

textBox.Text = "something"

Is it going to "override" the binding or is binding "stronger" then explicit assignment

Upvotes: 2

Views: 617

Answers (3)

Rohit Vats
Rohit Vats

Reputation: 81253

No it won't update your binding. Binding gets updated only when it comes from View, if you set it from your code behind, it will override the text but will break the binding. You can try this sample -

  • Place a textbox on your view and bind it's text property to some property in your viewmodel say value for this property is "Test"
  • Now place two button's on your view.
  • On first button click simply set the text of your textbox to something say "Button1".
  • You will notice that textbox text will now be "Button1" but still the value of your CLR property will be "Test".
  • Now on second button click, try to set your Viewmodel property to say "Button2". PropertyChanged will be fired but you won't notice any change in your textbox text.

If you want to update the binding, you have to set the Dependency property from your code behind like this-

textBox.SetCurrentValue(TextBox.TextProperty, "Button2");

where textBox is your name of the TextBox.

Upvotes: 2

Fischermaen
Fischermaen

Reputation: 12458

Just simply put the value you will like to be in the textbox in the "Text" memeber of the bound object. Otherwise the binding will be overwritten, as devdigital mentioned above.

Upvotes: 1

devdigital
devdigital

Reputation: 34349

No, the binding will be overwritten.

Upvotes: 6

Related Questions