In Code-Behind

This tutorial will walk you through the common tasks of adding and removing RadRatingItems programmatically.

Example 1 demonstrates a regular RadRating declaration:

Example 1: Declare a RadRating

<telerik:RadRating x:Name="radRating"/> 

Figure 1: Result from Example 1

RadRating

Adding RadRatingItems

In order to add new rating items to a RadRating control, you have to perform several simple steps:

  • Create an instance of the RadRatingItem class

  • Set its properties such as Content if you need so

  • Add it to the RadRating's Items collection

    Example 2: Adding RadRatingItems

        RadRatingItem ratingItem = new RadRatingItem(); 
        this.radRating.Items.Add(ratingItem); 
    
        Dim ratingItem As New RadRatingItem() 
        Me.radRating.Items.Add(ratingItem) 
    

Figure 2: Result from Example 2

RadRating

In order to clear the default rating items and add new you have to add one additional step to the previous:

  • Clear the Items collection of the RadRating control

  • Create an instance of the RadRatingItem class

  • Set its properties such as Content if you need so

  • Add it to the RadRating's Items collection

Example 3: Clearing the Items collection

this.radRating.Items.Clear();  
RadRatingItem ratingItem = new RadRatingItem(); 
ratingItem.Content = "1"; 
this.radRating.Items.Add(ratingItem); 
ratingItem = new RadRatingItem(); 
ratingItem.Content = "2"; 
this.radRating.Items.Add(ratingItem); 
Me.radRating.Items.Clear()  
Dim ratingItem As New RadRatingItem() 
ratingItem.Content = "1" 
Me.radRating.Items.Add(ratingItem) 
ratingItem As New RadRatingItem() 
ratingItem.Content = "2" 
Me.radRating.Items.Add(ratingItem) 

Figure 3: Result from Example 3

RadRating

Consider declaring rating items in XAML instead of adding them by code whenever it's possible. This includes situations when you know what items you need at design time.

Removing RadRatingItems

In order to remove a specific RadRatingItem, you should remove it from the RadRating's Items collection.

Example 4: Removing RadRatingItems

private void RemoveRatingItem( RadRatingItem itemToRemove ) 
{ 
   this.radRating.Items.Remove( itemToRemove ); 
} 
Private Sub RemoveRatingItem(ByVal itemToRemove As RadRatingItem) 
    Me.radRating.Items.Remove(itemToRemove) 
End Sub 
In this article