後,我有這樣的WPF綁定代碼:單向綁定停止工作目標手動更新
TestModel source = new TestModel();
TestModel target = new TestModel();
Bind(source, target, BindingMode.OneWay);
source.Attribute = "1";
AssertAreEqual(target.Attribute, "1");
target.Attribute = "foo";
source.Attribute = "2";
AssertAreEqual(target.Attribute, "2");
第二斷言失敗!這對我來說似乎很奇怪。
另外,我嘗試了'OneWayToSource'而不是'OneWay',並且所有的按預期工作。
Bind(source, target, BindingMode.OneWayToSource);
target.Attribute = "1";
AssertAreEqual(source.Attribute, "1");
source.Attribute = "foo";
target.Attribute = "2";
AssertAreEqual(source.Attribute, "2");
其他詳情:
void Bind(TestModel source, TestModel target, BindingMode mode)
{
Binding binding = new Binding();
binding.Source = source;
binding.Path = new PropertyPath(TestModel.AttributeProperty);
binding.Mode = mode;
BindingOperations.SetBinding(target, TestModel.AttributeProperty, binding);
}
class TestModel : DependencyObject
{
public static readonly DependencyProperty AttributeProperty =
DependencyProperty.Register("Attribute", typeof(string), typeof(TestModel), new PropertyMetadata(null));
public string Attribute
{
get { return (string)GetValue(AttributeProperty); }
set { SetValue(AttributeProperty, value); }
}
}
什麼是錯我的代碼?
它使風格設置throught樣式會覆蓋模板值,並且本地值將覆蓋樣式值等,但我沒有這一點。所以不確定這是否解釋了這個問題......非常感謝。 – alex2k8 2009-02-28 20:12:24
此外,這並不能解釋OneWay和OneWayToSource之間的區別 – alex2k8 2009-02-28 20:18:35