Creating A Range Slider in WPF
Creating A Range Slider in WPF
The idea is simple, the control should look like a slider but with two 'thumbs':
The way I chose to tackle this was, as always, to leverage existing controls wherever possible.
Composition is king in WPF and UserControls provide a very lightweight, cheap angle of attack here.
I decided to have a UserControl composed, primarily, of two Sliders stack on top of each other.
Logically, the RangeSlider would have four properties:
Minimum (double)
LowerValue (double)
UpperValue (double)
Maximum (double)
So the first thing to do is add these as dependency properties to our new UserControl (called
RangeSlider):
public double Minimum
{
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
Properties Done.
</UserControl>
Notice how we set a name on the UserControl which allows us to bind to him and access all his
properties - inluding our juicy new ones. Even, better, because the Bindings are two way any
updates propagate between the UserControl and the composed controls. Awesome. Functionally,
we already have most of our control!
Some rules need applying, e.g. the UpperValue should always be greater than or equal to the
LowerValue and I'd even like one slider to drag the other along if it bumps into it.
However, the most pressing problem at the moment is the fact that my control looks like this: