Skip to content

Commit f999378

Browse files
committed
Merge branch 'spiegelp-master'
2 parents fe8de2d + 244f1b1 commit f999378

File tree

9 files changed

+348
-10
lines changed

9 files changed

+348
-10
lines changed

MainDemo.Wpf/Buttons.xaml

Lines changed: 32 additions & 6 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">
@@ -24,12 +25,14 @@
2425
<RowDefinition Height="Auto" />
2526
<RowDefinition Height="Auto" />
2627
<RowDefinition Height="Auto" />
27-
<RowDefinition />
28-
<RowDefinition />
29-
<RowDefinition />
30-
<RowDefinition />
31-
<RowDefinition />
32-
<RowDefinition />
28+
<RowDefinition Height="Auto" />
29+
<RowDefinition Height="Auto" />
30+
<RowDefinition Height="Auto" />
31+
<RowDefinition Height="Auto" />
32+
<RowDefinition Height="Auto" />
33+
<RowDefinition Height="Auto" />
34+
<RowDefinition Height="Auto" />
35+
<RowDefinition Height="Auto" />
3336
</Grid.RowDefinitions>
3437
<TextBlock>Buttons</TextBlock>
3538
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0 24 0 0">
@@ -149,5 +152,28 @@
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 Value="3" x:Name="BasicRatingBar" />
158+
<TextBlock Text="{Binding ElementName=BasicRatingBar, Path=Value, StringFormat=Rating: {0}}" VerticalAlignment="Top" Margin="10,2,0,0" />
159+
<wpf:RatingBar x:Name="CustomRatingBar" Max="3" Value="2" Margin="24 0 0 0" Orientation="Vertical">
160+
<wpf:RatingBar.ValueItemTemplate>
161+
<DataTemplate DataType="system:Int32">
162+
<Grid>
163+
<Viewbox Width="24" Height="24">
164+
<Canvas Width="24" Height="24">
165+
<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"
166+
Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Control}, Path=Foreground}" />
167+
</Canvas>
168+
</Viewbox>
169+
<TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center"
170+
FontSize="8"
171+
Foreground="{DynamicResource PrimaryHueMidForegroundBrush}"/>
172+
</Grid>
173+
</DataTemplate>
174+
</wpf:RatingBar.ValueItemTemplate>
175+
</wpf:RatingBar>
176+
<TextBlock Text="{Binding ElementName=CustomRatingBar, Path=Value, StringFormat=Rating: {0}}" VerticalAlignment="Top" Margin="10,2,0,0" />
177+
</StackPanel>
152178
</Grid>
153179
</UserControl>

MainDemo.Wpf/Buttons.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public partial class Buttons : UserControl
2222
{
2323
public Buttons()
2424
{
25-
InitializeComponent();
25+
InitializeComponent();
2626
}
2727

2828
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+
}

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,6 +211,7 @@
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" />
211216
<Compile Include="Converters\SizeToRectConverter.cs" />
212217
<Compile Include="CustomPopupPlacementCallbackHelper.cs" />
@@ -235,6 +240,8 @@
235240
<DependentUpon>Settings.settings</DependentUpon>
236241
<DesignTimeSharedInput>True</DesignTimeSharedInput>
237242
</Compile>
243+
<Compile Include="RatingBar.cs" />
244+
<Compile Include="RatingBarButton.cs" />
238245
<Compile Include="TextFieldAssist.cs" />
239246
<Compile Include="Converters\TextFieldHintVisibilityConverter.cs" />
240247
<Compile Include="TimePicker.cs" />

MaterialDesignThemes.Wpf/RatingBar.cs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Windows;
2+
using System.Windows.Controls.Primitives;
3+
4+
namespace MaterialDesignThemes.Wpf
5+
{
6+
public class RatingBarButton : ButtonBase
7+
{
8+
static RatingBarButton()
9+
{
10+
DefaultStyleKeyProperty.OverrideMetadata(typeof(RatingBarButton), new FrameworkPropertyMetadata(typeof(RatingBarButton)));
11+
}
12+
13+
private static readonly DependencyPropertyKey ValuePropertyKey =
14+
DependencyProperty.RegisterReadOnly(
15+
"Value", typeof (int), typeof (RatingBarButton),
16+
new PropertyMetadata(default(int)));
17+
18+
public static readonly DependencyProperty ValueProperty =
19+
ValuePropertyKey.DependencyProperty;
20+
21+
public int Value
22+
{
23+
get { return (int) GetValue(ValueProperty); }
24+
internal set { SetValue(ValuePropertyKey, value); }
25+
}
26+
27+
private static readonly DependencyPropertyKey IsWithinValuePropertyKey =
28+
DependencyProperty.RegisterReadOnly(
29+
"IsWithinSelectedValue", typeof (bool), typeof (RatingBarButton),
30+
new PropertyMetadata(default(bool)));
31+
32+
public static readonly DependencyProperty IsWithinSelectedValueProperty =
33+
IsWithinValuePropertyKey.DependencyProperty;
34+
35+
public bool IsWithinSelectedValue
36+
{
37+
get { return (bool) GetValue(IsWithinSelectedValueProperty); }
38+
internal set { SetValue(IsWithinValuePropertyKey, value); }
39+
}
40+
}
41+
}

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.Defaults.xaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf">
34

45
<!-- use this resource dictionary to set up the most common themese for standard controls -->
56

@@ -17,7 +18,7 @@
1718
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Menu.xaml" />
1819
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.PasswordBox.xaml" />
1920
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ProgressBar.xaml" />
20-
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.RadioButton.xaml" />
21+
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.RadioButton.xaml" />
2122
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ScrollBar.xaml" />
2223
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ScrollViewer.xaml" />
2324
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/materialdesigntheme.slider.xaml" />
@@ -43,7 +44,7 @@
4344
<Style TargetType="{x:Type ListBox}" BasedOn="{StaticResource MaterialDesignListBox}" />
4445
<Style TargetType="{x:Type PasswordBox}" BasedOn="{StaticResource MaterialDesignPasswordBox}" />
4546
<Style TargetType="{x:Type ProgressBar}" BasedOn="{StaticResource MaterialDesignLinearProgressBar}" />
46-
<Style TargetType="{x:Type RadioButton}" BasedOn="{StaticResource MaterialDesignRadioButton}" />
47+
<Style TargetType="{x:Type RadioButton}" BasedOn="{StaticResource MaterialDesignRadioButton}" />
4748
<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource MaterialDesignScrollBar}" />
4849
<Style TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource MaterialDesignScrollViewer}" />
4950
<Style TargetType="{x:Type Slider}" BasedOn="{StaticResource MaterialDesignSlider}" />

0 commit comments

Comments
 (0)