WPF 数据绑定:BindingOperations.SetBinding() 不使用依赖属性的值
我有一个自己的依赖属性 Target.Height
,使用 BindingOperations.SetBinding()
绑定到普通属性 Source.Height
。更新 Target.Height
属性应该更新 Source.Height
属性。但不使用依赖属性的实际值,而是使用依赖属性的默认值。这是预期的行为吗?
感谢您的任何提示。我使用的代码:
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 ....
I have a own dependency property Target.Height
bound to a normal property Source.Height
using BindingOperations.SetBinding()
. Updating the Target.Height
property should update the Source.Height
property. But not the actual value of the dependency property is used rather the default value of the dependency property. Is this the intended behavior?
Thanks for any hints. Code I use:
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 ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WPF 将 Binding 作为依赖属性的值。当您设置绑定时,您实际上是将当前的属性值替换为新的属性值。在 DependencyObject.SetValueCommon 的末尾,您可能会找到执行此操作的代码。在那里我们可以看到 WPF 获取一个默认值,然后使用表达式标记将其设置为当前属性值,然后附加 BindingExpression,它使用当前属性值(默认值)更新源。
WPF puts Binding as values of dependency properties. When you setting up a binding you actually replaces your current property value with a new one. At the end of the DependencyObject.SetValueCommon you may find a code that did it. There we can see that WPF gets a default value, then set it as a current property value with expression marker, and then attach BindingExpression which updates the source using the current property value - the default value.