Remove binding in WPF using code

I would like to use data binding when displaying data in a TextBox. I basically do as:

public void ShowRandomObject(IRandomObject randomObject) { Binding binding = new Binding {Source = randomObject, Path = new PropertyPath("Name")}; txtName.SetBinding(TextBox.TextProperty, binding); } 

I can not find a way to unlink. I will call this method many different objects, but the TextBox will remain the same. Is there a way to remove the previous binding or is it done automatically when I establish a new binding?

+45
c # wpf
09 Oct '08 at 9:05
source share
4 answers

When available

 BindingOperations.ClearBinding(txtName, TextBox.TextProperty) 

For older versions of SilverLight, but not reliable, as noted in the comments:

 txtName.SetBinding(TextBox.TextProperty, null); 
+30
09 Oct '08 at 9:07
source share
β€” -

As an alternative:

 BindingOperations.ClearBinding(txtName, TextBox.TextProperty) 
+85
Oct 09 '08 at 15:49
source share

What about:

 this.ClearValue(TextBox.TextProperty); 

This is much cleaner, I think;)

+17
09 Oct '08 at 11:44
source share

How about just

 txtName.Text = txtName.Text; 

You will need to set the value after clearing it. This works in SL4 at least.

0
Jun 04 '10 at
source share



All Articles