如何使装订方面的DependencyProperty值强制? [英] How do I make Binding respect DependencyProperty value coercion?
问题描述
我有一个CoerceValueCallback一个DependencyProperty的控制。
此属性绑定到一个模型对象的属性。
在设置控件属性,导致强制绑定推动的不受胁迫价值模型对象的值。在控件的属性值正确强制。
我怎么绑定到强制值推到模型对象?
无效初始化()
{
UIObject的UI =新UIObject类();
ModelObject M =新ModelObject();
熔点= 4; 结合B =新绑定(P);
b.Source =米;
b.Mode = BindingMode.TwoWay;
的Debug.WriteLine(SetBinding);
//设置绑定将模型值推到UI
ui.SetBinding(UIObject.PProperty,B); //设置UI值会导致强迫,但只有在UI中。
//通过结合推到模型中的值不强制。
的Debug.WriteLine(设置到-4);
ui.P = -4; Debug.Assert的(ui.P == 0);
//绑定是双向的DP值被强制为0。
Debug.Assert的(熔点== 0); // 不对。这将是-4。为什么???
}类UIObject类:FrameworkElement的
{
公共静态只读的DependencyProperty PProperty =
DependencyProperty.Register(P的typeof(INT)的typeof(UIObject类),
新FrameworkPropertyMetadata(
新PropertyChangedCallback(OnPChanged)
新CoerceValueCallback(CoerceP))); 公众诠释P
{
{返回(INT)的GetValue(PProperty); }
集合{的SetValue(PProperty,值); }
} 私有静态无效OnPChanged(DependencyObject的D,DependencyPropertyChangedEventArgs E)
{
的Debug.WriteLine(typeof运算(UIObject类)+ + + e.OldValue到+ e.NewValue.P从);
} 私有静态对象CoerceP(DependencyObject的发件人,对象的值)
{
INT P =(int)的值;
如果(第℃,)
{
的Debug.WriteLine(typeof运算(UIObject类)+ P +.P从强制0);
p值= 0;
}
回磷;
}
}类ModelObject
{
私人诠释磷;
公众诠释P
{
得到
{
的Debug.WriteLine(这+.P返回+ this.p);
返回this.p;
}
组
{
的Debug.WriteLine(本+ + + this.p.P从+改为到+值);
this.p =价值;
}
}
}
我不认为要挟回调意味着是一个双行道。其中一个解决方法是将更新要挟回调的内部模型的价值。
I have a control with a DependencyProperty with a CoerceValueCallback. This property is bound to a property on a model object.
When setting the control property to a value that causes coercion the Binding pushes the uncoerced value to the model object. The property value on the control is coerced correctly.
How do I get the Binding to push the coerced value to the model object?
void Initialize()
{
UIObject ui = new UIObject();
ModelObject m = new ModelObject();
m.P = 4;
Binding b = new Binding("P");
b.Source = m;
b.Mode = BindingMode.TwoWay;
Debug.WriteLine("SetBinding");
// setting the binding will push the model value to the UI
ui.SetBinding(UIObject.PProperty, b);
// Setting the UI value will result in coercion but only in the UI.
// The value pushed to the model through the binding is not coerced.
Debug.WriteLine("Set to -4");
ui.P = -4;
Debug.Assert(ui.P == 0);
// The binding is TwoWay, the DP value is coerced to 0.
Debug.Assert(m.P == 0); // Not true. This will be -4. Why???
}
class UIObject : FrameworkElement
{
public static readonly DependencyProperty PProperty =
DependencyProperty.Register("P", typeof(int), typeof(UIObject),
new FrameworkPropertyMetadata(
new PropertyChangedCallback(OnPChanged),
new CoerceValueCallback(CoerceP)));
public int P
{
get { return (int)GetValue(PProperty); }
set { SetValue(PProperty, value); }
}
private static void OnPChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine(typeof(UIObject) + ".P changed from " + e.OldValue + " to " + e.NewValue);
}
private static object CoerceP(DependencyObject sender, object value)
{
int p = (int)value;
if (p < 0)
{
Debug.WriteLine(typeof(UIObject) + ".P coerced from " + p + " to 0");
p = 0;
}
return p;
}
}
class ModelObject
{
private int p;
public int P
{
get
{
Debug.WriteLine(this + ".P returned " + this.p);
return this.p;
}
set
{
Debug.WriteLine(this + ".P changed from +" + this.p + " to " + value);
this.p = value;
}
}
}
I don't think the coerce callback is meant to be a two-way street. One workaround would be to update the model's value inside of the coerce callback.
这篇关于如何使装订方面的DependencyProperty值强制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!