Rating Control was introduced in the WPF Toolkit February 2010 release. In this article, we will see how we can use the Rating Control.
Crating WPF Application Project
Fire up Visual Studio 2008 and create a WPF application and name the project as RatingInWPF.
We can add the rating control by adding the following DLL from the WPF Toolkit directory.
Or, we can add it in Expression Blend.
After adding the Rating control, add a few RatingItem Controls. Suppose we need to rate out of 5, then add 5 RatingItems.
By default, content comes with Rating Item control, so remove the content value.
The following figure shows the hierarchy in object and timeline pane.
Now, we will use the Rating Control to rate Movies displayed in a list and store it.
So, let's create some sample Data and Bind it to the ListBox.
The above class represents our Movie structure.
As you can see in the above code display, we have used ObservableCollection, because at runtime, we would be seeing what value we have stored for each Movie.
The above code display shows the DataTemplate for our data to be bound.
Now, run the application and we will test the ListBox with sample data.
Now, we will have two events - one from RatingControl and another from ListBox; as follows, and add the below code in the respective event handlers.
The highlighted code has to be entered in the Constructor. For some reason, the event cannot be created in XAML.
That's it; we are ready to test our application.
As we are using ObservableCollection the change in the Collection is instantly affected. So, changing the Rating Value will change in the Collection.
I hope this article helps.