2015-04-24 59 views
0

我能想到的最簡單的例子是以下幾點:雙向綁定不更新控制

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); 

當我開始我的應用程序所期望的文本框儘快其他文本框的文本更改更新他們的文字。但是,在更改其他文本框時,源文本框和目標文本框都不會更新其文本。 是否有這種奇怪的行爲的原因,並可能有解決方法?

(因爲我不能用XAML,具體原因where綁定總是爲我工作)


更新:當改變文本框首次之一的文本,在輸出窗口說線程0x ####已退出,代碼爲259(0x103)。

回答

1

您正在使用錯誤的DependencyProperty。使用TextBox.TextProperty

+0

好時

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

複製的代碼按預期工作。這是尷尬......謝謝:) – Cubinator73

+0

我們都在這兩個之間做,我敢肯定。 – bdimag