|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Linq; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using System.Windows; |
| 10 | +using System.Windows.Controls; |
| 11 | +using System.Windows.Data; |
| 12 | +using System.Windows.Input; |
| 13 | + |
| 14 | +namespace MaterialDesignThemes.Wpf |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A custom control implementing a rating bar. |
| 18 | + /// The icon aka content may be set as a DataTemplate via the ButtonContentTemplate property. |
| 19 | + /// </summary> |
| 20 | + public class RatingBar : Control |
| 21 | + { |
| 22 | + public static RoutedCommand SelectRatingCommand = new RoutedCommand(); |
| 23 | + |
| 24 | + static RatingBar() |
| 25 | + { |
| 26 | + DefaultStyleKeyProperty.OverrideMetadata(typeof(RatingBar), new FrameworkPropertyMetadata(typeof(RatingBar))); |
| 27 | + } |
| 28 | + |
| 29 | + private readonly ObservableCollection<RatingBarButton> _ratingButtonsInternal = new ObservableCollection<RatingBarButton>(); |
| 30 | + private readonly ReadOnlyObservableCollection<RatingBarButton> _ratingButtons; |
| 31 | + |
| 32 | + public RatingBar() |
| 33 | + { |
| 34 | + CommandBindings.Add(new CommandBinding(SelectRatingCommand, SelectItemHandler)); |
| 35 | + _ratingButtons = new ReadOnlyObservableCollection<RatingBarButton>(_ratingButtonsInternal); |
| 36 | + } |
| 37 | + |
| 38 | + private void SelectItemHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs) |
| 39 | + { |
| 40 | + if (executedRoutedEventArgs.Parameter is int) |
| 41 | + Value = (int) executedRoutedEventArgs.Parameter; |
| 42 | + } |
| 43 | + |
| 44 | + public static readonly DependencyProperty MinProperty = DependencyProperty.Register( |
| 45 | + "Min", typeof (int), typeof (RatingBar), new PropertyMetadata(1, MinPropertyChangedCallback)); |
| 46 | + |
| 47 | + public int Min |
| 48 | + { |
| 49 | + get { return (int) GetValue(MinProperty); } |
| 50 | + set { SetValue(MinProperty, value); } |
| 51 | + } |
| 52 | + |
| 53 | + public static readonly DependencyProperty MaxProperty = DependencyProperty.Register( |
| 54 | + "Max", typeof (int), typeof (RatingBar), new PropertyMetadata(5, MaxPropertyChangedCallback)); |
| 55 | + |
| 56 | + public int Max |
| 57 | + { |
| 58 | + get { return (int) GetValue(MaxProperty); } |
| 59 | + set { SetValue(MaxProperty, value); } |
| 60 | + } |
| 61 | + |
| 62 | + public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( |
| 63 | + "Value", typeof (int), typeof (RatingBar), new PropertyMetadata(0, ValuePropertyChangedCallback)); |
| 64 | + |
| 65 | + private static void ValuePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) |
| 66 | + { |
| 67 | + foreach (var button in ((RatingBar) dependencyObject).RatingButtons) |
| 68 | + button.IsWithinSelectedValue = button.Value <= (int)dependencyPropertyChangedEventArgs.NewValue; |
| 69 | + } |
| 70 | + |
| 71 | + public int Value |
| 72 | + { |
| 73 | + get { return (int) GetValue(ValueProperty); } |
| 74 | + set { SetValue(ValueProperty, value); } |
| 75 | + } |
| 76 | + |
| 77 | + public ReadOnlyObservableCollection<RatingBarButton> RatingButtons => _ratingButtons; |
| 78 | + |
| 79 | + public static readonly DependencyProperty ValueItemContainerButtonStyleProperty = DependencyProperty.Register( |
| 80 | + "ValueItemContainerButtonStyle", typeof (Style), typeof (RatingBar), new PropertyMetadata(default(Style))); |
| 81 | + |
| 82 | + public Style ValueItemContainerButtonStyle |
| 83 | + { |
| 84 | + get { return (Style) GetValue(ValueItemContainerButtonStyleProperty); } |
| 85 | + set { SetValue(ValueItemContainerButtonStyleProperty, value); } |
| 86 | + } |
| 87 | + |
| 88 | + public static readonly DependencyProperty ValueItemTemplateProperty = DependencyProperty.Register( |
| 89 | + "ValueItemTemplate", typeof (DataTemplate), typeof (RatingBar), new PropertyMetadata(default(DataTemplate))); |
| 90 | + |
| 91 | + public DataTemplate ValueItemTemplate |
| 92 | + { |
| 93 | + get { return (DataTemplate) GetValue(ValueItemTemplateProperty); } |
| 94 | + set { SetValue(ValueItemTemplateProperty, value); } |
| 95 | + } |
| 96 | + |
| 97 | + public static readonly DependencyProperty ValueItemTemplateSelectorProperty = DependencyProperty.Register( |
| 98 | + "ValueItemTemplateSelector", typeof (DataTemplateSelector), typeof (RatingBar), new PropertyMetadata(default(DataTemplateSelector))); |
| 99 | + |
| 100 | + public DataTemplateSelector ValueItemTemplateSelector |
| 101 | + { |
| 102 | + get { return (DataTemplateSelector) GetValue(ValueItemTemplateSelectorProperty); } |
| 103 | + set { SetValue(ValueItemTemplateSelectorProperty, value); } |
| 104 | + } |
| 105 | + |
| 106 | + public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register( |
| 107 | + "Orientation", typeof (Orientation), typeof (RatingBar), new PropertyMetadata(default(Orientation))); |
| 108 | + |
| 109 | + public Orientation Orientation |
| 110 | + { |
| 111 | + get { return (Orientation) GetValue(OrientationProperty); } |
| 112 | + set { SetValue(OrientationProperty, value); } |
| 113 | + } |
| 114 | + |
| 115 | + private static void MaxPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) |
| 116 | + { |
| 117 | + ((RatingBar)dependencyObject).RebuildButtons(); |
| 118 | + } |
| 119 | + |
| 120 | + private static void MinPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) |
| 121 | + { |
| 122 | + ((RatingBar)dependencyObject).RebuildButtons(); |
| 123 | + } |
| 124 | + |
| 125 | + private void RebuildButtons() |
| 126 | + { |
| 127 | + _ratingButtonsInternal.Clear(); |
| 128 | + for (var i = Min; i <= Max; i++) |
| 129 | + { |
| 130 | + _ratingButtonsInternal.Add(new RatingBarButton |
| 131 | + { |
| 132 | + Content = i, |
| 133 | + ContentTemplate = ValueItemTemplate, |
| 134 | + ContentTemplateSelector = ValueItemTemplateSelector, |
| 135 | + IsWithinSelectedValue = i <= Value, |
| 136 | + Style = ValueItemContainerButtonStyle, |
| 137 | + Value = i, |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public override void OnApplyTemplate() |
| 143 | + { |
| 144 | + RebuildButtons(); |
| 145 | + |
| 146 | + base.OnApplyTemplate(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments