Remove binding in WPF using code


Question

I would like to use databinding when displaying data in a TextBox. I'm basically doing like:

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

I can't seem to find a way to unset the binding. I will be calling this method with a lot of different objects but the TextBox will remain the same. Is there a way to remove the previous binding or is this done automatically when I set the new binding?

1
51
10/9/2008 9:05:28 AM

Accepted Answer

When available

BindingOperations.ClearBinding(txtName, TextBox.TextProperty)

For older SilverLight versions, but not reliable as stated in comments:

txtName.SetBinding(TextBox.TextProperty, null);

C# 6.0 features enabled

this.btnFinish.ClearBinding(ButtonBase.CommandProperty);
39
3/23/2018 8:10:40 AM

Alternately:

BindingOperations.ClearBinding(txtName, TextBox.TextProperty)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon