我有一個自己的依賴屬性Target.Height
綁定到一個正常的屬性Source.Height
使用BindingOperations.SetBinding()
。更新Target.Height
屬性應更新Source.Height
屬性。但不是使用依賴項屬性的實際值,而是使用依賴項屬性的默認值。這是預期的行爲嗎?WPF數據綁定:BindingOperations.SetBinding()不使用依賴屬性的值
感謝您的任何提示。我使用的代碼:
public class Source
{
private int m_height;
public int Height
{
get { return m_height; }
set { m_height = value; }
}
}
public class Target : DependencyObject
{
public static readonly DependencyProperty HeightProperty;
static Target()
{
Target.HeightProperty =
DependencyProperty.Register("Height", typeof(int), typeof(Target),
new PropertyMetadata(666)); //the default value
}
public int Height
{
get { return (int)GetValue(Target.HeightProperty); }
set { SetValue(Target.HeightProperty, value); }
}
}
Source source = new Source();
Target target = new Target();
target.Height = 100;
Binding heightBinding = new Binding("Height");
heightBinding.Source = source;
heightBinding.Mode = BindingMode.OneWayToSource;
BindingOperations.SetBinding(target, Target.HeightProperty, heightBinding);
//target.Height and source.Height is now 666 instead of 100 ....
這絕對是一種奇怪的行爲。我能夠在LINQpad中重現它。我預計價值將是100,而不是666.期待看到答案。 – NathanAW
複製粘貼和調試你的源代碼,這兩個屬性後,你的最後一行代碼,是666 ...奇怪... – stukselbax