Skip to content

Commit 29fdc83

Browse files
committed
added custom control implementing a rating bar
1 parent c854e4d commit 29fdc83

File tree

8 files changed

+397
-0
lines changed

8 files changed

+397
-0
lines changed

MainDemo.Wpf/Buttons.xaml

Lines changed: 10 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,12 @@
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" Rating="3" PathData="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" />
158+
<TextBlock Text="Rating: " VerticalAlignment="Center" />
159+
<TextBlock x:Name="ratingTextBlock" Text="{Binding Path=Rating, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" />
160+
<wpf:RatingBar Rating="3" IsEnabled="False" Margin="40,0,0,0" PathData="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" />
161+
</StackPanel>
152162
</Grid>
153163
</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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
<SubType>Designer</SubType>
6565
<Generator>MSBuild:Compile</Generator>
6666
</Page>
67+
<Page Include="RatingBar.xaml">
68+
<SubType>Designer</SubType>
69+
<Generator>MSBuild:Compile</Generator>
70+
</Page>
6771
<Page Include="Themes\Generic.xaml">
6872
<Generator>MSBuild:Compile</Generator>
6973
<SubType>Designer</SubType>
@@ -203,7 +207,9 @@
203207
<Compile Include="Converters\ClockItemIsCheckedConverter.cs" />
204208
<Compile Include="Converters\ClockLineConverter.cs" />
205209
<Compile Include="Converters\NotConverter.cs" />
210+
<Compile Include="Converters\NotNullToVisibilityConverter.cs" />
206211
<Compile Include="Converters\NotZeroToVisibilityConverter.cs" />
212+
<Compile Include="Converters\RatingToBoolConverter.cs" />
207213
<Compile Include="Converters\SizeToRectConverter.cs" />
208214
<Compile Include="CustomPopupPlacementCallbackHelper.cs" />
209215
<Compile Include="DataGridAssist.cs" />
@@ -231,6 +237,9 @@
231237
<DependentUpon>Settings.settings</DependentUpon>
232238
<DesignTimeSharedInput>True</DesignTimeSharedInput>
233239
</Compile>
240+
<Compile Include="RatingBar.xaml.cs">
241+
<DependentUpon>RatingBar.xaml</DependentUpon>
242+
</Compile>
234243
<Compile Include="TextFieldAssist.cs" />
235244
<Compile Include="Converters\TextFieldHintVisibilityConverter.cs" />
236245
<Compile Include="TimePicker.cs" />

0 commit comments

Comments
 (0)