Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 159963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:57:04+00:00 2026-05-11T10:57:04+00:00

I have a control with a DependencyProperty with a CoerceValueCallback. This property is bound

  • 0

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;         }     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:57:05+00:00Added an answer on May 11, 2026 at 10:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control that is having issues binding to a dependency property
I have a user control that has a Title property bound to the Text
I have a custom control in WPF. In this I have a DependencyProperty of
I have a user control that i am using inside a DataTemplate , this
I have a user control that exposes a property of type ImageSource. I want
I want to have a User Control that takes a collection of People (property
I have a custom control that has a property that is an ObservableCollection of
I have my custom control named FileSelectDialog with Dependency Property: public static readonly DependencyProperty
I have a wpf user control which exposes an IEnumerable ItemsSource DependencyProperty. I bind
Is there a way to Minimize an external application that I don't have control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.