Skip to content

Commit 84b3bc7

Browse files
dhilmathyKeboo
authored andcommitted
Added ValueChanged event for RatingBar - Fixes #1027 (#1029)
Added ValueChangedEvent for RatingBar
1 parent b787bc1 commit 84b3bc7

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

MainDemo.Wpf/Buttons.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@
572572
<TextBlock Margin="0 32 0 0" Grid.Row="9" Style="{StaticResource MaterialDesignHeadlineTextBlock}">Rating bar</TextBlock>
573573
<StackPanel Grid.Row="10" Margin="0 16 0 0" Orientation="Horizontal">
574574
<smtx:XamlDisplay Key="buttons_58" VerticalContentAlignment="Top" Margin="5 0 0 5">
575-
<materialDesign:RatingBar Value="3" x:Name="BasicRatingBar" />
575+
<materialDesign:RatingBar Value="3" x:Name="BasicRatingBar" ValueChanged="BasicRatingBar_ValueChanged"/>
576576
</smtx:XamlDisplay>
577577
<TextBlock Text="{Binding ElementName=BasicRatingBar, Path=Value, StringFormat=Rating: {0}}" VerticalAlignment="Top" Margin="10,2,0,0" />
578578
<smtx:XamlDisplay Key="buttons_59" Margin="24 0 0 5">

MainDemo.Wpf/Buttons.xaml.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System;
2+
using System.Diagnostics;
63
using System.Windows;
74
using System.Windows.Controls;
8-
using System.Windows.Data;
9-
using System.Windows.Documents;
105
using System.Windows.Input;
11-
using System.Windows.Media;
12-
using System.Windows.Media.Imaging;
13-
using System.Windows.Navigation;
14-
using System.Windows.Shapes;
156
using MaterialDesignColors.WpfExample.Domain;
167

178
namespace MaterialDesignColors.WpfExample
@@ -24,7 +15,6 @@ public partial class Buttons : UserControl
2415
public Buttons()
2516
{
2617
InitializeComponent();
27-
2818
FloatingActionDemoCommand = new AnotherCommandImplementation(Execute);
2919
}
3020

@@ -36,9 +26,9 @@ private void Execute(object o)
3626
}
3727

3828
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
39-
{
29+
{
4030
Console.WriteLine("Just checking we haven't suppressed the button.");
41-
}
31+
}
4232

4333
private void PopupBox_OnOpened(object sender, RoutedEventArgs e)
4434
{
@@ -58,7 +48,11 @@ private void CountingButton_OnClick(object sender, RoutedEventArgs e)
5848
var next = int.Parse(CountingBadge.Badge.ToString()) + 1;
5949

6050
CountingBadge.Badge = next < 21 ? (object)next : null;
61-
51+
}
52+
53+
private void BasicRatingBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
54+
{
55+
Debug.WriteLine($"BasicRatingBar value changed from {e.OldValue} to {e.NewValue}.");
6256
}
6357
}
6458
}

MaterialDesignThemes.Wpf/RatingBar.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
31
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;
92
using System.Windows;
103
using System.Windows.Controls;
11-
using System.Windows.Data;
124
using System.Windows.Input;
135

146
namespace MaterialDesignThemes.Wpf
@@ -64,8 +56,10 @@ public int Max
6456

6557
private static void ValuePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
6658
{
67-
foreach (var button in ((RatingBar) dependencyObject).RatingButtons)
59+
var ratingBar = (RatingBar)dependencyObject;
60+
foreach (var button in ratingBar.RatingButtons)
6861
button.IsWithinSelectedValue = button.Value <= (int)dependencyPropertyChangedEventArgs.NewValue;
62+
OnValueChanged(ratingBar, dependencyPropertyChangedEventArgs);
6963
}
7064

7165
public int Value
@@ -74,6 +68,30 @@ public int Value
7468
set { SetValue(ValueProperty, value); }
7569
}
7670

71+
public static readonly RoutedEvent ValueChangedEvent =
72+
EventManager.RegisterRoutedEvent(
73+
nameof(Value),
74+
RoutingStrategy.Bubble,
75+
typeof(RoutedPropertyChangedEventHandler<int>),
76+
typeof(RatingBar));
77+
78+
public event RoutedPropertyChangedEventHandler<int> ValueChanged
79+
{
80+
add => AddHandler(ValueChangedEvent, value);
81+
remove => RemoveHandler(ValueChangedEvent, value);
82+
}
83+
84+
private static void OnValueChanged(
85+
DependencyObject d, DependencyPropertyChangedEventArgs e)
86+
{
87+
var instance = (RatingBar)d;
88+
var args = new RoutedPropertyChangedEventArgs<int>(
89+
(int)e.OldValue,
90+
(int)e.NewValue)
91+
{ RoutedEvent = ValueChangedEvent };
92+
instance.RaiseEvent(args);
93+
}
94+
7795
public ReadOnlyObservableCollection<RatingBarButton> RatingButtons => _ratingButtons;
7896

7997
public static readonly DependencyProperty ValueItemContainerButtonStyleProperty = DependencyProperty.Register(

0 commit comments

Comments
 (0)