Cubi73
Cubi73

Reputation: 1951

TwoWay binding doesn't update controls

The simplest example I could think of was the following:

TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();

this.Content = new StackPanel() { Children = { tb1, tb2 } };

Binding binding = new Binding("Text") {
    Mode = BindingMode.TwoWay,
    Source = tb1,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};

tb2.SetBinding(TextBlock.TextProperty, binding);

When I start my application I expect the textboxes to update their text as soon as the other textbox's text changes. But neither the source textbox nor the target textbox updates its text when changing the other textbox. Is there a reason for this strange behavior and might there be a workaround?

(For specific reasons I cannot use XAML, where bindings always worked for me)


Update: When changing the text of one of the textboxes for the first time, the output window says The thread 0x#### has exited with code 259 (0x103).

Upvotes: 0

Views: 93

Answers (1)

bdimag
bdimag

Reputation: 963

You're using the wrong DependencyProperty.

tb2.SetBinding(**TextBlock**.TextProperty, binding);

Copied code works as expected when using TextBox.TextProperty

Upvotes: 1

Related Questions