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 6104177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:48:44+00:00 2026-05-23T13:48:44+00:00

In Silverlight there is no DependencyObject.CoerceValue. I am looking for an alternative, to do

  • 0

In Silverlight there is no DependencyObject.CoerceValue. I am looking for an alternative, to do the following WPF-Code also in Silverlight.

The situation:
There is a class Range, which has several DependencyProperties: MinimumProperty, MaximumProperty, LowerValueProperty and UpperValueProperty.

Minimum may never be greater then Maximum, Maximum never be smaller than Minimum. Moreover LowerValue and UpperValue have to be in between Minimum and Maximum, whereas LowerValue always smaller then UpperValue.

All DependencyProperties are implemented like this (in WPF):

public static new readonly DependencyProperty MinimumProperty =
     DependencyProperty.Register("Minimum",
     typeof(double),
     typeof(Range),
     new FrameworkPropertyMetadata(0d,
       new PropertyChangedCallback(Range.OnMinimumChanged),
       new CoerceValueCallback(Range.CoerceMinimum)),
     new ValidateValueCallback(Range.ValidateMinimum));

public new double Minimum
    {
      get { return (double)base.GetValue(MinimumProperty); }
      set { base.SetValue(MinimumProperty, value); }
    }

The coercion in WPF is done like that:

private static object CoerceMinimum(DependencyObject source, object value)
{
  Range r = source as Range;
  double maximum = r.Maximum;
  double val = (double)value;
  if (val > maximum)
  {
    return maximum;
  }
  return value;
}

PropertyChangedCallback looks like this:

private static void OnMinimumChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
  Range r = source as Range;
  r.CoerceValue(LowerValueProperty);
  r.CoerceValue(UpperValueProperty);
  r.CoerceValue(MaximumProperty);
}

The ValidateValueCallback doesn’t matter in this case. The other Callbacks are similar to the shown code.

In WPF this runs good. For example I set (in XAML)

<Range LowerValue="12" Minimum="10" UpperValue="15" Maximum="20" />

all values are correct. The order doesn’t matter!

But in Silverlight I do not get it running.

First step is the workaround for CoerceValueCallback. I raise the coercion in PropertyChangedCallback, like this:

private static void OnMinimumChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
  Range r = source as Range;
  double newVal = (double)e.NewValue;
  double coercedVal = (double)CoerceMinimum(source, newVal);
  if (coercedVal != newVal)
  {
    r.Minimum = coercecVal;
    return;
  }

  r.CoerceValue(LowerValueProperty);
  r.CoerceValue(UpperValueProperty);
  r.CoerceValue(MaximumProperty);
}

If now Minimum is set to an value, the CoerceMinimum is still executed and the Minimum-Coercion done well.

But the last three lines do not compile, because DependencyObject has no CoerceValue-Method. And exactly this is the position where I am at one’s wits’ end.

How do I raise the Coercion for LowerValue, UpperValue and Maximum on MinimumChanged?
Or is there another way to ensure, that the order of initialization does not matter and all properties are set correctly (assuming that the condition are fulfilled)?

Thanks in advance!

  • 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. Editorial Team
    Editorial Team
    2026-05-23T13:48:44+00:00Added an answer on May 23, 2026 at 1:48 pm

    I got a solution 🙂 I am sure, it isnt completed yet. I’ve still to debug all possible orders, but I think most already done.

    I post just the code for LowerValue, the others are nearly the same:

    • OnUpperValueChanged raise CoerceUpperValueChanged and
      CoerceLowerValueChanged.
    • OnMinimumChanged raise CoerceMinimum,
      CoerceUpperValueChanged and
      CoerceLowerValueChanged.
    • OnMaximumChanged raise CoerceMaximum, CoerceUpperValueChanged and CoerceLowerValueChanged.

    The coercion of Minimum and Maximum just easy. If new value is greater than Maximum (or smaller than Minimum) take Maximum (or Minimum), else take the new value.

    Here’s the code:

    private static void OnLowerValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
      Range r = source as Range;
    
      if (r._levelsFromRootCall == 0)
      {
        r._requestedLowerVal = (double)e.NewValue;
      }
    
      r._levelsFromRootCall++;
      r.CoerceLowerValue();
    
      r.CoerceUpperValue();
      r._levelsFromRootCall--;
    }
    
    private void CoerceLowerValue()
    {
      double minimum = this.Minimum;
      double maximum = this.Maximum;
      double lowerVal = this.LowerValue;
      double upperVal = this.UpperValue;
      if (lowerVal != _requestedLowerVal && _requestedLowerVal >= minimum && _requestedLowerVal <= maximum)
      {
        if (_requestedLowerVal <= upperVal)
        {
          base.SetValue(LowerValueProperty, _requestedLowerVal);
        }
        else
        {
          base.SetValue(LowerValueProperty, upperVal);
        }
      }
      else
      {
        if (lowerVal < minimum)
        {
          base.SetValue(LowerValueProperty, minimum);
        }
        else if (lowerVal > upperVal || _requestedLowerVal > upperVal)
        {
          base.SetValue(LowerValueProperty, upperVal);
        }
        else if (lowerVal > maximum)
        {
          base.SetValue(LowerValueProperty, maximum);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.