Published

Filtering values with a silverlight slider and linq

Normally, when we want to filter a list of values in a web page, we introduce the filter condition in an input type and then we should press a button to apply the condition. If we are lucky enough the web page will include some Ajax features to avoid doing a post back. If not, we will need to do the post back a recharge the whole page. This doesn’t seem to be the most optimum way of working. However, lately I’ve seen some web pages that have introduced Slider controls as a filter mechanism. The way in which you apply the filter it’s very intuitive, you only need to move the thumb along its track and the filter will be applied.

In my opinion the Slider mechanism provides a much better user experience and looks nicer than the conventional input type + button. With Silverlight 1.1 and Linq we can implement this in a few minutes.

The first we need to do it’s to create the Slider. Instead of adding it directly in a Silverlight project we will build it as “Silverlight Control”, this will allow us reusing the Slider in other projects.

The XAML we will create it’s very simple, we will have a Path that will be our Thumb, a Line as the track and a Rectangle that we will use to be able to move the Thumb across the Track by clicking directly in the desired position.

In the class associated to this XAML we add the necessary properties to delimit the values accepted by the Slider: MinValue, MaxValue and also to know and specify the current value. Note in the lines from 11 to 19 that when we set the “Value” property we perform two method calls: OnValueChanged and UpdatePosition. The first one will be in charge of firing the “ValueChanged” event you can see below. The second one will allow to update the position of the Thumb when we set the property directly by code, C# or XAML, if we don’t add this part the only way to update the Thumb position would be using the mouse.

1:publicdouble MaxValue { get; set; }

2:

3:publicdouble MinValue { get; set; }

4:

5:publicdouble Value

6: {

7: get

8: {

9:return _value;

10: }

11: set

12: {

13:if (_value != value)

14: {

15: _value = value;

16: OnValueChanged();

17: UpdatePosition(_value);

18: }

19: }

20: }

In addition to the properties we will include an event “ValueChanged” to notify users that the value of the Slider has been modified.

1:publicevent EventHandler ValueChanged;

2:

3:protectedvirtualvoid OnValueChanged()

4: {

5: EventHandler handler = ValueChanged;

6:if (handler != null)

7: ValueChanged(this, EventArgs.Empty);

8:

9: } double _value;

In order to be able to move the Thumb, it’s necessary we control the mouse events that occur over our Slider, for that we only need to add the necessary event handlers for the events MouseMove, MouseLeftButtonDown and MouseLeftButtonUp and to implement basic Drag & Drop functionality.

1:void element_MouseLeftButtonDown(object sender, MouseEventArgs e)

2: {

3: MoveThumb(e.GetPosition(null).X);

4: }

5:

6:void thumb_MouseMove(object sender, MouseEventArgs e)

7: {

8:if (this.isDragging)

9: {

10: MoveThumb(e.GetPosition(null).X);

11: }

12: }

13:

14:void thumb_MouseLeftButtonUp(object sender, MouseEventArgs e)

15: {

16:this.isDragging = false;

17: thumbElement.ReleaseMouseCapture();

18: }

19:

20:void thumb_MouseLeftButtonDown(object sender, MouseEventArgs e)

21: {

22:this.isDragging = true;

23: thumbElement.CaptureMouse();

24: }

You can see the complete implementation of the MoveThumb function downloading the full code for this post.

This is all what we need to do to have a basic Slider control, of course for production environments you will need to add some more functionality to handle different sizes for the Slider, colors, etc. Now that we have the slider, let’s see how we can make use of it.

The first thing we need to do is to create a Silverlight project and add a reference to our library. Then, the way in which we can add custom controls in our XAML pages is very easy. We only need to import the namespace and add the XAML code for the control doing something like this (Note that we can make use of our custom properties directly in the XAML code).

In the code below what we do is to create a few products with Id and Price (just imagine you retrieved from somewhere else) and we keep them in a list.

Now we only need to do the final touch, we add a handler for the event ValueChanged we created before in the Slider control. In the handler we add a Linq query that we will use to filter the products we want to display based on the price of the product and the “Value” property of the Slider. To finish we just iterate the elements and we create a TextBlock element for each product that we will add dynamically in the Canvas “container”.