开发者

Silverlight: Missing DependencyObject.CoerceValue

开发者 https://www.devze.com 2023-03-16 13:34 出处:网络
In Silverlight there is no DependencyObject.CoerceValue. I am looking for an alternative, to do the following WPF-Code also in Silverlight.

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!


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

精彩评论

暂无评论...
验证码 换一张
取 消