Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make Binding respect DependencyProperty value coercion?
    primarykey
    data
    text
    <p>I have a control with a DependencyProperty with a CoerceValueCallback. This property is bound to a property on a model object.</p> <p>When setting the control property to a value that causes coercion the Binding pushes the <strong>uncoerced</strong> value to the model object. The property value on the control is coerced correctly.</p> <p>How do I get the Binding to push the <strong>coerced</strong> value to the model object?</p> <pre><code>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 &lt; 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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload