RadRating - NumberOfItemsToGenerate property is not working with binding

3 posts, 0 answers
  1. catqbat
    catqbat avatar
    9 posts
    Member since:
    Jun 2013

    Posted 22 Apr 2014 Link to this post

    Hello,

    I'm using RadRating Control for silverlight (2014.1.421.1050). The control is part of my user control and I would like to set number of stars dynamically using binding to dependency property. This however seems not to work - each time the number of stars equals five.

    Here are some code snippets:

    This is dependency property for my user control, put in code behind:

    public partial class my_rating_control : UserControl
    {
    public my_rating_control()
    {
    InitializeComponent();

    }

    public int MaxRating
    {
    get { return (int)GetValue(MaxRatingProperty); }
    set { SetValue(MaxRatingProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MaxRating.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MaxRatingProperty =
    DependencyProperty.Register("MaxRating", typeof(int), typeof(my_rating_control), new PropertyMetadata(5));

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
    Rating3.NumberOfItemsToGenerate = 7;
    }
    }

    this is xaml for my user control:

    <UserControl x:Class="rad_rating_test.my_rating_control"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400"
        xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
                 x:Name="UserControl" Loaded="UserControl_Loaded">
        
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="Rating set to 7 from xaml" Grid.Row="0" Grid.Column="0" />
            <telerik:RadRating x:Name="Rating1"  Grid.Row="0" HorizontalAlignment="Left" Height="23" Grid.Column="1"  VerticalAlignment="Top"
                                 NumberOfItemsToGenerate="7"  />

            <TextBlock Text="Rating set to 7 via binding" Grid.Row="1" Grid.Column="0" />

            <telerik:RadRating x:Name="Rating2" Grid.Row="1" HorizontalAlignment="Left" Height="23" Grid.Column="1"  VerticalAlignment="Top"
                                 NumberOfItemsToGenerate="{Binding MaxRating, ElementName=UserControl}"  />

            <TextBlock Text="Rating set to 7 after loaded event" Grid.Row="2" Grid.Column="0" />

            <telerik:RadRating x:Name="Rating3"  Grid.Row="2" HorizontalAlignment="Left" Height="23" Grid.Column="1"  VerticalAlignment="Top"
                                />

        </Grid>
    </UserControl>

    this is how I use this control in my main page:

    <UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:rad_rating_test" x:Class="rad_rating_test.MainPage"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

    <local:my_rating_control Margin="0" MaxRating="7"/>

    </Grid>
    </UserControl>

    If you run the application, only the first rating has seven stars: the one which is explicitly set to 7 in xaml: NumberOfItemsToGenerate="7". The rest have 5 stars. 

    Could you please tell me how to achieve the desired effect? 





































  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 25 Apr 2014 Link to this post

    Hi Agata,

    I can confirm that this is a bug in the RadRating component. I logged it in our feedback portal where you can track its status. I also updated your Telerik points as a small gesture of compensation for the inconvenience caused.

    As a workaround you can bind the ItemsSource property of the rating control to the MaxRating property. Then with a converter create a new empty collection with length set to the value of the MaxRating property.
    <telerik:RadRating ItemsSource="{Binding MaxRating, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource ratingToItemsSourceConverter}}" />
    ......
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new object[(int)value];           
    }

    In addition I attached a sample project which demonstrates this approach. 

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. DevCraft banner
  4. catqbat
    catqbat avatar
    9 posts
    Member since:
    Jun 2013

    Posted 25 Apr 2014 in reply to Martin Link to this post

    Thank you! :)
Back to Top