I want to model the behavior of a RangeSlider
, using a class RangeSliderModel
with properties describing the sizes (widths) for a given set of values.
Then, I created an UserControl where the position and size of some rectangles should represent those properties, and some instances of Thumb
from which the DragDelta
events are used to resize these rectangles and enable the calculation of a new value for a RangeSliderModel
.
At last, this UserControl has a DependencyProperty
of type RangeSliderModel
:
public RangeSliderModel Model
{
get
{
return (RangeSliderModel)GetValue(RangeSliderModelProperty);
// or instead calculate a RangeSliderModel from children sizes.
}
set
{
// this IS NOT called when UserControl loads
SetValue(RangeSliderModelProperty, value);
// here I could take the value and calculate new sizes for the children.
}
}
public static readonly DependencyProperty RangeSliderModelProperty =
DependencyProperty.Register("RangeSliderModel",
typeof(RangeSliderModel),
typeof(MyUserControl),
new PropertyMetadata(ModelPropertyChanged));
private static void ModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// this IS called when UserControl loads.
RangeSliderModel model = e.NewValue as RangeSliderModel;
}
I was expecting to override the SetValue
and GetValue
properties, but even when I bind this DependencyProperty to a corresponding Source in ViewModel, the setter is never called! And I can't quite understand why PropertyChangedHandler is called instead, and much less what I should do with this information - I was expecting to use just GetValue
and SetValue
in a way similar to a ValueConverter.