|
| 1 | +--- |
| 2 | +title: Resizing RadRating Item Visuals in Telerik Themes with Glyph Icons |
| 3 | +description: Learn how to customize the size of rating visuals in the RadRating control for WPF applications. |
| 4 | +type: how-to |
| 5 | +page_title: How to Resize Rating Item Symbols in RadRating for WPF |
| 6 | +slug: kb-rating-resize-item-symbols |
| 7 | +tags: radrating, wpf, resize, ratingitem, glyph, fontsize, style |
| 8 | +res_type: kb |
| 9 | +ticketid: 1652842 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Product | Version | |
| 15 | +| --- | --- | |
| 16 | +| RadRating for WPF | 2024.2.514 | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +How to resize the star shapes in RadRating in newer Telerik themes (Material and later). In these themes, the rating visuals uses `RadGlyph` visuals. This article shows how to change their `FontSize`. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To change the size of the stars in `RadRating`, you can use a custom attached property that finds the `RadGlyph` elements and sets their `FontSize`. The property can be assigned to the `RadRatingItem` elements using an implicit style. |
| 25 | + |
| 26 | +#### __[C#] Defining the attached property__ |
| 27 | +{{region kb-rating-resize-item-symbols-0}} |
| 28 | + public static class RadGlyphUtilities |
| 29 | + { |
| 30 | + public static double GetFontSize(DependencyObject obj) |
| 31 | + { |
| 32 | + return (double)obj.GetValue(FontSizeProperty); |
| 33 | + } |
| 34 | + |
| 35 | + public static void SetFontSize(DependencyObject obj, double value) |
| 36 | + { |
| 37 | + obj.SetValue(FontSizeProperty, value); |
| 38 | + } |
| 39 | + |
| 40 | + public static readonly DependencyProperty FontSizeProperty = |
| 41 | + DependencyProperty.RegisterAttached( |
| 42 | + "FontSize", |
| 43 | + typeof(double), |
| 44 | + typeof(RadGlyphUtilities), |
| 45 | + new PropertyMetadata(-1d, OnFontSizeChanged)); |
| 46 | + |
| 47 | + private static void OnFontSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 48 | + { |
| 49 | + if (d is RadGlyph) |
| 50 | + { |
| 51 | + ((RadGlyph)d).FontSize = (double)e.NewValue; |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + var element = (FrameworkElement)d; |
| 56 | + if (element.IsLoaded) |
| 57 | + { |
| 58 | + SetGlyphsFontSize(element, (double)e.NewValue); |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + element.Loaded += Element_Loaded; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void Element_Loaded(object sender, RoutedEventArgs e) |
| 67 | + { |
| 68 | + var element = (FrameworkElement)sender; |
| 69 | + SetGlyphsFontSize(element, GetFontSize(element)); |
| 70 | + element.Loaded -= Element_Loaded; |
| 71 | + } |
| 72 | + |
| 73 | + private static void SetGlyphsFontSize(FrameworkElement parent, double fontSize) |
| 74 | + { |
| 75 | + var glyphs = parent.ChildrenOfType<RadGlyph>(); |
| 76 | + foreach (RadGlyph glyph in glyphs) |
| 77 | + { |
| 78 | + glyph.SetCurrentValue(RadGlyph.FontSizeProperty, fontSize); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +{{endregion}} |
| 83 | + |
| 84 | +#### __[XAML] Using the attached property__ |
| 85 | +{{region kb-rating-resize-item-symbols-0}} |
| 86 | + <Window.Resources> |
| 87 | + <Style TargetType="{x:Type telerik:RadRatingItem}"> |
| 88 | + <Setter Property="MaxWidth" Value="15" /> |
| 89 | + <Setter Property="local:RadGlyphUtilities.FontSize" Value="14" /> |
| 90 | + </Style> |
| 91 | + </Window.Resources> |
| 92 | +{{endregion}} |
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments