Skip to content

Commit 5f44794

Browse files
committed
Merge branch 'master' of https://github.com/spiegelp/MaterialDesignInXamlToolkit into spiegelp-master
2 parents fe8de2d + 5f7276d commit 5f44794

File tree

9 files changed

+327
-0
lines changed

9 files changed

+327
-0
lines changed

MainDemo.Wpf/Buttons.xaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:MaterialDesignColors.WpfExample"
77
xmlns:wpfExample="clr-namespace:MaterialDesignColors.WpfExample"
8+
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
89
xmlns:system="clr-namespace:System;assembly=mscorlib"
910
mc:Ignorable="d"
1011
d:DesignHeight="300" d:DesignWidth="300">
@@ -30,6 +31,8 @@
3031
<RowDefinition />
3132
<RowDefinition />
3233
<RowDefinition />
34+
<RowDefinition />
35+
<RowDefinition />
3336
</Grid.RowDefinitions>
3437
<TextBlock>Buttons</TextBlock>
3538
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0 24 0 0">
@@ -149,5 +152,24 @@
149152
</Viewbox>
150153
</ToggleButton>
151154
</StackPanel>
155+
<TextBlock Margin="0 24 0 0" Grid.Row="9">Rating bar</TextBlock>
156+
<StackPanel Grid.Row="10" Margin="0 16 0 0" Orientation="Horizontal">
157+
<wpf:RatingBar x:Name="ratingBar" MaxRating="5" Rating="2">
158+
<wpf:RatingBar.RatingItemTemplate>
159+
<DataTemplate>
160+
<Viewbox Width="21" Height="21">
161+
<Canvas Width="24" Height="24">
162+
<Path Data="M12,21.35L10.55,20.03C5.4,15.36 2,12.27 2,8.5C2,5.41 4.42,3 7.5,3C9.24,3 10.91,3.81 12,5.08C13.09,3.81 14.76,3 16.5,3C19.58,3 22,5.41 22,8.5C22,12.27 18.6,15.36 13.45,20.03L12,21.35Z"
163+
Fill="{StaticResource PrimaryHueMidBrush}" />
164+
</Canvas>
165+
</Viewbox>
166+
</DataTemplate>
167+
</wpf:RatingBar.RatingItemTemplate>
168+
</wpf:RatingBar>
169+
<TextBlock Text="Rating: " VerticalAlignment="Center" Margin="10,0,0,0" />
170+
<TextBlock x:Name="ratingTextBlock" Text="{Binding Path=Rating, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" />
171+
<wpf:RatingBar MaxRating="7" Rating="5" IsEnabled="False" Margin="20,0,0,0" Width="224" />
172+
<TextBlock Text="disabled" VerticalAlignment="Center" Margin="10,0,0,0" />
173+
</StackPanel>
152174
</Grid>
153175
</UserControl>

MainDemo.Wpf/Buttons.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public partial class Buttons : UserControl
2323
public Buttons()
2424
{
2525
InitializeComponent();
26+
27+
ratingTextBlock.DataContext = ratingBar;
2628
}
2729

2830
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows;
4+
using System.Windows.Data;
5+
6+
namespace MaterialDesignThemes.Wpf.Converters
7+
{
8+
public class NotNullToVisibilityConverter : IValueConverter
9+
{
10+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
11+
{
12+
return value != null ? Visibility.Visible : Visibility.Collapsed;
13+
}
14+
15+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
16+
{
17+
return Binding.DoNothing;
18+
}
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows.Data;
8+
9+
namespace MaterialDesignThemes.Wpf.Converters
10+
{
11+
public class RatingToBoolConverter : IValueConverter
12+
{
13+
public int Rating
14+
{
15+
get; set;
16+
}
17+
18+
public RatingToBoolConverter() {
19+
Rating = 3;
20+
}
21+
22+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
23+
{
24+
if (value == null)
25+
{
26+
return false;
27+
}
28+
29+
return ((int)value) >= Rating;
30+
}
31+
32+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
33+
{
34+
return Binding.DoNothing;
35+
}
36+
}
37+
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@
116116
<SubType>Designer</SubType>
117117
<Generator>MSBuild:Compile</Generator>
118118
</Page>
119+
<Page Include="Themes\MaterialDesignTheme.RatingBar.xaml">
120+
<SubType>Designer</SubType>
121+
<Generator>MSBuild:Compile</Generator>
122+
</Page>
119123
<Page Include="Themes\MaterialDesignTheme.ScrollBar.xaml">
120124
<SubType>Designer</SubType>
121125
<Generator>MSBuild:Compile</Generator>
@@ -207,7 +211,9 @@
207211
<Compile Include="Converters\ClockItemIsCheckedConverter.cs" />
208212
<Compile Include="Converters\ClockLineConverter.cs" />
209213
<Compile Include="Converters\NotConverter.cs" />
214+
<Compile Include="Converters\NotNullToVisibilityConverter.cs" />
210215
<Compile Include="Converters\NotZeroToVisibilityConverter.cs" />
216+
<Compile Include="Converters\RatingToBoolConverter.cs" />
211217
<Compile Include="Converters\SizeToRectConverter.cs" />
212218
<Compile Include="CustomPopupPlacementCallbackHelper.cs" />
213219
<Compile Include="DataGridAssist.cs" />
@@ -235,6 +241,7 @@
235241
<DependentUpon>Settings.settings</DependentUpon>
236242
<DesignTimeSharedInput>True</DesignTimeSharedInput>
237243
</Compile>
244+
<Compile Include="RatingBar.cs" />
238245
<Compile Include="TextFieldAssist.cs" />
239246
<Compile Include="Converters\TextFieldHintVisibilityConverter.cs" />
240247
<Compile Include="TimePicker.cs" />

MaterialDesignThemes.Wpf/RatingBar.cs

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
10+
namespace MaterialDesignThemes.Wpf
11+
{
12+
/// <summary>
13+
/// A custom control implementing a rating bar.
14+
/// The icon aka content may be set as a DataTemplate via the ButtonContentTemplate property.
15+
/// </summary>
16+
public class RatingBar : Control
17+
{
18+
private const string RatingBarGridPartName = "PART_ratingBarGrid";
19+
20+
public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxRating", typeof(int), typeof(RatingBar));
21+
22+
public int MaxRating
23+
{
24+
get { return (int)GetValue(MaxValueProperty); }
25+
26+
set
27+
{
28+
if (value < 1)
29+
{
30+
throw new ArgumentException("the maximum value must be greater than 1");
31+
}
32+
33+
SetValue(MaxValueProperty, value);
34+
35+
RebuildUi();
36+
}
37+
}
38+
39+
public static readonly DependencyProperty RatingProperty = DependencyProperty.Register("Rating", typeof(int), typeof(RatingBar));
40+
41+
public int Rating
42+
{
43+
get { return (int)GetValue(RatingProperty); }
44+
45+
set
46+
{
47+
int rating = value;
48+
49+
if (rating < 1)
50+
{
51+
rating = 1;
52+
}
53+
54+
if (rating > MaxRating)
55+
{
56+
rating = MaxRating;
57+
}
58+
59+
SetValue(RatingProperty, rating);
60+
61+
UpdateButtonOpacity();
62+
}
63+
}
64+
65+
private static readonly DependencyProperty RatingItemTemplateProperty = DependencyProperty.Register("RatingItemTemplate", typeof(DataTemplate), typeof(RatingBar));
66+
67+
public DataTemplate RatingItemTemplate
68+
{
69+
get { return (DataTemplate)GetValue(RatingItemTemplateProperty); }
70+
71+
set
72+
{
73+
SetValue(RatingItemTemplateProperty, value);
74+
75+
RebuildUi();
76+
}
77+
}
78+
79+
private Button[] _ratingButtons;
80+
81+
static RatingBar()
82+
{
83+
DefaultStyleKeyProperty.OverrideMetadata(typeof(RatingBar), new FrameworkPropertyMetadata(typeof(RatingBar)));
84+
}
85+
86+
public RatingBar()
87+
{
88+
MaxRating = 5;
89+
Rating = 3;
90+
}
91+
92+
public override void OnApplyTemplate()
93+
{
94+
RebuildUi();
95+
96+
base.OnApplyTemplate();
97+
}
98+
99+
private void RebuildUi()
100+
{
101+
// rebuild the grid as basic layout
102+
Grid grid = GetTemplateChild(RatingBarGridPartName) as Grid;
103+
104+
if (grid != null)
105+
{
106+
grid.ColumnDefinitions.Clear();
107+
grid.Children.Clear();
108+
109+
for (int i = 0; i < MaxRating; i++)
110+
{
111+
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
112+
}
113+
114+
// create buttons
115+
if (_ratingButtons != null)
116+
{
117+
foreach (Button button in _ratingButtons)
118+
{
119+
button.Click -= RatingButtonClick;
120+
}
121+
}
122+
123+
_ratingButtons = new Button[MaxRating];
124+
125+
for (int i = 0; i < MaxRating; i++)
126+
{
127+
Button button = new Button();
128+
button.ContentTemplate = RatingItemTemplate;
129+
130+
button.DataContext = this;
131+
Binding enabledBinding = new Binding("IsEnabled");
132+
enabledBinding.Mode = BindingMode.OneWay;
133+
enabledBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
134+
BindingOperations.SetBinding(button, Button.IsEnabledProperty, enabledBinding);
135+
136+
button.Click += RatingButtonClick;
137+
138+
_ratingButtons[i] = button;
139+
140+
Grid.SetColumn(button, i);
141+
Grid.SetRow(button, 0);
142+
grid.Children.Add(button);
143+
144+
Style style = null;
145+
Style basedOn = TryFindResource("MaterialDesignRatingBarButton") as Style;
146+
147+
if (basedOn != null)
148+
{
149+
style = new Style(typeof(Button), basedOn);
150+
}
151+
else
152+
{
153+
style = new Style(typeof(Button));
154+
}
155+
156+
button.Style = style;
157+
}
158+
159+
UpdateButtonOpacity();
160+
}
161+
}
162+
163+
private void UpdateButtonOpacity()
164+
{
165+
if (_ratingButtons != null)
166+
{
167+
for (int i = 0; i < _ratingButtons.Length; i++)
168+
{
169+
if ((i + 1) <= Rating)
170+
{
171+
_ratingButtons[i].Opacity = 1.0;
172+
}
173+
else
174+
{
175+
_ratingButtons[i].Opacity = 0.5;
176+
}
177+
}
178+
}
179+
}
180+
181+
private int GetRatingForButton(Button ratingButton)
182+
{
183+
for (int i = 0; i < _ratingButtons.Length; i++)
184+
{
185+
if (_ratingButtons[i] == ratingButton)
186+
{
187+
return i + 1;
188+
}
189+
}
190+
191+
return 0;
192+
}
193+
194+
private void RatingButtonClick(object sender, RoutedEventArgs args)
195+
{
196+
Rating = GetRatingForButton(sender as Button);
197+
}
198+
}
199+
}

MaterialDesignThemes.Wpf/Themes/Generic.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
-->
1616
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Card.xaml" />
1717
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Clock.xaml" />
18+
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.RatingBar.xaml" />
1819
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.TimePicker.xaml" />
1920
</ResourceDictionary.MergedDictionaries>
2021

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Button.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@
212212
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
213213
</Style>
214214

215+
<Style x:Key="MaterialDesignRatingBarButton" TargetType="{x:Type Button}" BasedOn="{StaticResource MaterialDesignToolButton}">
216+
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidBrush}"/>
217+
<Setter Property="HorizontalAlignment" Value="Stretch" />
218+
<Setter Property="VerticalAlignment" Value="Stretch" />
219+
</Style>
220+
215221
<Style x:Key="MaterialDesignFloatingActionButton" TargetType="{x:Type Button}">
216222
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
217223
<Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}"/>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf">
4+
5+
<Style TargetType="{x:Type wpf:RatingBar}">
6+
<Setter Property="Height" Value="32"/>
7+
<Setter Property="Width" Value="160" />
8+
<Setter Property="Template">
9+
<Setter.Value>
10+
<ControlTemplate TargetType="{x:Type wpf:RatingBar}">
11+
<Grid x:Name="PART_ratingBarGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="100*" />
14+
</Grid.RowDefinitions>
15+
</Grid>
16+
</ControlTemplate>
17+
</Setter.Value>
18+
</Setter>
19+
<Setter Property="RatingItemTemplate">
20+
<Setter.Value>
21+
<DataTemplate>
22+
<Viewbox Width="21" Height="21">
23+
<Canvas Width="24" Height="24">
24+
<Path Data="M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z"
25+
Fill="Black" />
26+
</Canvas>
27+
</Viewbox>
28+
</DataTemplate>
29+
</Setter.Value>
30+
</Setter>
31+
</Style>
32+
33+
</ResourceDictionary>

0 commit comments

Comments
 (0)