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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use JAXB's XmlAdapters. Availible since JAXB 2.0 (JDK… May 11, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer You're comparing a string to a string pointer, and that's… May 11, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer You could try passing the "this" to the function: <asp:TextBox… May 11, 2026 at 10:43 pm

Related Questions

I am starting out with Silverlight. I want to display a list of messages
I have a Silverlight custom control with two properties; Text and Id. I have
I have this DependencyProperty which holds an entity with a property that is a
I have a UserControl which I'm using to display a list of UIElement s.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.