I need to implement a Ruler and I have a CustomControl RangeSlider which is derived from Control and has several DependencyProperties (e.g. Minimum, Maximum, LowerValue, UpperValue).
public static readonly DependencyProperty LowerValueProperty =
DependencyProperty.Register("LowerValue",
typeof(double),
typeof(RangeSlider),
new FrameworkPropertyMetadata(0d, OnLowerValueChanged, CoerceLowerValue));
public double LowerValue
{
get { return (double)base.GetValue(LowerValueProperty); }
set { base.SetValue(LowerValueProperty, value); }
}
In Xaml, the Ruler has a sub control Scale (derived from Canvas), which needs the same Properties to draw the Scaling. If LowerValue or UpperValue changed, the Scale should repaint. So I add
public static readonly DependencyProperty LowerValueProperty =
RangeSlider.LowerValueProperty.AddOwner(typeof(Scale), new FrameworkPropertyMetadata(1d));
When I move the thumb, something should raised in Scale. First I implemented OnRender(DrawingContext) and added to the LowerValueProperty in RangeSlider FrameworkPropertyMetadataOptions. But it doesn’t matter, nothing happens. Moreover I do not want to use OnRender(DrawingContext), because it’s not available in Silverlight.
Furthermore I wonder why the OnLowerValueChanged-Method in RangeSlider is raised but not the setter of LowerValue (at least the debugger does not break in there).
So, is there a way for Silverlight & WPF to raise a method automatically in Scale, when LowerValue in RangeSlider changed (except raising from OnLowerValueChanged)?
Note: The above code is the WPF-Code. In Silverlight there are of course some difference, because Silverlight does not support among other things Coercion.
Because the code should run in Silverlight and WPF, I do not want to use OnRender(DrawingContext).
If I understand correctly you want to be able to perform some logic in propertiers, that are just wrappers around GetValue & SetValue methods. Problem is they are only used when you create XAML and specify bindings for those properties. Behind the scenes they are never called anymore, instead GetValue & SetValue are used.
Thats why I think (still not sure I understand question clearly) you have to either use event handlers for
ValueChangedevent (for example) inside your control and there is no other way to intercept calls to GetValue & SetValue.Or you can use
PropertyChangedCallbackwith DependencyProperties that is called after DPs are changed and place your code there