如何使装订方面的DependencyProperty值强制?

编程入门 行业动态 更新时间:2024-10-20 03:47:59
本文介绍了如何使装订方面的DependencyProperty值强制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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值强制?

本文发布于:2023-11-28 15:27:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642955.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DependencyProperty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!