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

Related Questions

I have a chart control in XAML, and datapoints that are bind to the
I have a custom control with that test-code. Then I put my control on
I have custom control where i want to show some items. In generic.xaml defined
We need to assign the Name property at the runtime using the data binding.
I have a user control containing a listbox. I want to bind to the
I have a user control as follows: <UserControl x:Class=CaseDatabase.Controls.SearchResultControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:d=http://schemas.microsoft.com/expression/blend/2008 xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 mc:Ignorable=d
I have successfully created a number of UserControls with various functionality setting the DataContext
Well..its working BUT only when user creates a property that is of type List.
It's hard to explain but i'll do my best. I wanted to have a
Is it possible to bind data in the wrong direction? I want a value

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.