Skip to content

Commit a9beaaa

Browse files
committed
RadioButton and CheckBox use custom foreground
1 parent 6dff589 commit a9beaaa

File tree

5 files changed

+74
-26
lines changed

5 files changed

+74
-26
lines changed

src/MaterialDesign3.Demo.Wpf/Toggles.xaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@
119119
<smtx:XamlDisplay Margin="0,5,8,8"
120120
VerticalAlignment="Center"
121121
UniqueKey="buttons_42221">
122-
<RadioButton Content="Custom RadioButton Size" materialDesign:RadioButtonAssist.RadioButtonSize="40" Style="{StaticResource MaterialDesignRadioButton}" />
122+
<RadioButton Content="Custom RadioButton Size and foreground" materialDesign:RadioButtonAssist.RadioButtonSize="40" IsChecked="False" materialDesign:RadioButtonAssist.RadioButtonForeground="Blue" />
123+
</smtx:XamlDisplay>
124+
<smtx:XamlDisplay Margin="0,5,8,8"
125+
VerticalAlignment="Center"
126+
UniqueKey="buttons_42222">
127+
<RadioButton Content="Custom RadioButton Size and foreground for UserStyle" materialDesign:RadioButtonAssist.RadioButtonSize="30" IsChecked="True" Style="{DynamicResource MaterialDesignUserForegroundRadioButton}" materialDesign:RadioButtonAssist.RadioButtonForeground="Blue" />
123128
</smtx:XamlDisplay>
124129

125130
<smtx:XamlDisplay Margin="0,5,8,8"
@@ -425,9 +430,12 @@
425430
VerticalAlignment="Top"
426431
UniqueKey="checkboxes_2">
427432
<StackPanel Margin="8,0">
428-
<CheckBox materialDesign:CheckBoxAssist.CheckBoxSize="30"
433+
<CheckBox materialDesign:CheckBoxAssist.CheckBoxSize="30"
429434
Content="Custom Size"
430435
IsChecked="True" />
436+
<CheckBox materialDesign:CheckBoxAssist.CheckBoxSize="30" materialDesign:CheckBoxAssist.CheckBoxForeground="Red"
437+
Content="Custom Size and foreground" IsChecked="True"
438+
/>
431439
<CheckBox materialDesign:RippleAssist.IsDisabled="True"
432440
Content="Ripple disabled"
433441
IsChecked="True" />
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1+
using System.Windows.Media;
2+
13
namespace MaterialDesignThemes.Wpf;
24

35
public class CheckBoxAssist
46
{
57
private const double DefaultCheckBoxSize = 18.0;
68

79
#region AttachedProperty : CheckBoxSizeProperty
8-
public static readonly DependencyProperty CheckBoxSizeProperty
9-
= DependencyProperty.RegisterAttached("CheckBoxSize", typeof(double), typeof(CheckBoxAssist), new PropertyMetadata(DefaultCheckBoxSize));
10+
public static readonly DependencyProperty CheckBoxSizeProperty = DependencyProperty.RegisterAttached(
11+
"CheckBoxSize",
12+
typeof(double),
13+
typeof(CheckBoxAssist),
14+
new PropertyMetadata(DefaultCheckBoxSize)
15+
);
1016

1117
public static double GetCheckBoxSize(CheckBox element) => (double)element.GetValue(CheckBoxSizeProperty);
18+
1219
public static void SetCheckBoxSize(CheckBox element, double checkBoxSize) => element.SetValue(CheckBoxSizeProperty, checkBoxSize);
1320
#endregion
21+
22+
23+
24+
#region AttachedProperty : CheckBoxForegroundProperty
25+
public static readonly DependencyProperty CheckBoxForegroundProperty = DependencyProperty.RegisterAttached(
26+
"CheckBoxForeground",
27+
typeof(Brush),
28+
typeof(CheckBoxAssist),
29+
null
30+
);
31+
32+
public static Brush GetCheckBoxForeground(CheckBox element) => (Brush)element.GetValue(CheckBoxForegroundProperty);
33+
34+
public static void SetCheckBoxForeground(CheckBox element, Brush checkBoxForeground) =>
35+
element.SetValue(CheckBoxForegroundProperty, checkBoxForeground);
36+
#endregion
1437
}
Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
namespace MaterialDesignThemes.Wpf
1+
using System.Windows.Media;
2+
3+
namespace MaterialDesignThemes.Wpf
24
{
35
public class RadioButtonAssist
46
{
57
private const double DefaultRadioButtonSize = 18.0;
68

79
#region AttachedProperty : RadioButtonSizeProperty
8-
public static readonly DependencyProperty RadioButtonSizeProperty =
9-
DependencyProperty.RegisterAttached(
10-
"RadioButtonSize",
11-
typeof(double),
12-
typeof(RadioButtonAssist),
13-
new PropertyMetadata(DefaultRadioButtonSize)
14-
);
10+
public static readonly DependencyProperty RadioButtonSizeProperty = DependencyProperty.RegisterAttached(
11+
"RadioButtonSize",
12+
typeof(double),
13+
typeof(RadioButtonAssist),
14+
new PropertyMetadata(DefaultRadioButtonSize)
15+
);
16+
17+
public static double GetRadioButtonSize(RadioButton element) => (double)element.GetValue(RadioButtonSizeProperty);
18+
19+
public static void SetRadioButtonSize(RadioButton element, double RadioButtonSize) =>
20+
element.SetValue(RadioButtonSizeProperty, RadioButtonSize);
21+
#endregion
22+
23+
#region AttachedProperty :RadioButtonForegroundProperty
24+
public static readonly DependencyProperty RadioButtonForegroundProperty = DependencyProperty.RegisterAttached(
25+
"RadioButtonForeground",
26+
typeof(Brush),
27+
typeof(RadioButtonAssist),
28+
null
29+
);
1530

16-
public static double GetRadioButtonSize(RadioButton element) =>
17-
(double)element.GetValue(RadioButtonSizeProperty);
31+
public static Brush GetRadioButtonForeground(RadioButton element) => (Brush)element.GetValue(RadioButtonForegroundProperty);
1832

19-
public static void SetRadioButtonSize(RadioButton element, double checkBoxSize) =>
20-
element.SetValue(RadioButtonSizeProperty, checkBoxSize);
33+
public static void SetRadioButtonForeground(RadioButton element, Brush radioButtonForeground) =>
34+
element.SetValue(RadioButtonForegroundProperty, radioButtonForeground);
2135
#endregion
2236
}
2337
}

src/MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.CheckBox.xaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Setter Property="BorderThickness" Value="1" />
7171
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
7272
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />
73+
<Setter Property="wpf:CheckBoxAssist.CheckBoxForeground" Value="{DynamicResource MaterialDesign.Brush.Primary}"/>
7374
<Setter Property="Template">
7475
<Setter.Value>
7576
<ControlTemplate TargetType="{x:Type CheckBox}">
@@ -111,13 +112,13 @@
111112
<Canvas Width="24" Height="24">
112113
<Path x:Name="Graphic"
113114
Data="M19,3H5C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5C21,3.89 20.1,3 19,3M19,5V19H5V5H19Z"
114-
Fill="{DynamicResource MaterialDesign.Brush.ForegroundLight}" />
115+
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:CheckBoxAssist.CheckBoxForeground)}" />
115116
<Ellipse x:Name="InteractionEllipse"
116117
Canvas.Left="12"
117118
Canvas.Top="12"
118119
Width="0"
119120
Height="0"
120-
Fill="{TemplateBinding Foreground}"
121+
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:CheckBoxAssist.CheckBoxForeground)}"
121122
IsHitTestVisible="False"
122123
Opacity="0"
123124
RenderTransformOrigin="0.5,0.5">
@@ -158,7 +159,7 @@
158159
<Trigger Property="IsPressed" Value="true" />
159160
<Trigger Property="IsChecked" Value="true">
160161
<Setter TargetName="Graphic" Property="Data" Value="M10,17L5,12L6.41,10.58L10,14.17L17.59,6.58L19,8M19,3H5C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5C21,3.89 20.1,3 19,3Z" />
161-
<Setter TargetName="Graphic" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
162+
<Setter TargetName="Graphic" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:CheckBoxAssist.CheckBoxForeground)}" />
162163
</Trigger>
163164
<Trigger Property="IsEnabled" Value="false">
164165
<Setter Property="Opacity" Value="0.56" />
@@ -205,6 +206,7 @@
205206
<Setter Property="BorderThickness" Value="1" />
206207
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
207208
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />
209+
<Setter Property="wpf:CheckBoxAssist.CheckBoxForeground" Value="{DynamicResource MaterialDesign.Brush.Primary}"/>
208210
<Setter Property="Template">
209211
<Setter.Value>
210212
<ControlTemplate TargetType="{x:Type CheckBox}">
@@ -246,13 +248,13 @@
246248
<Canvas Width="24" Height="24">
247249
<Path x:Name="Graphic"
248250
Data="M19,3H5C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V5C21,3.89 20.1,3 19,3M19,5V19H5V5H19Z"
249-
Fill="{TemplateBinding Foreground}" />
251+
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:CheckBoxAssist.CheckBoxForeground)}" />
250252
<Ellipse x:Name="InteractionEllipse"
251253
Canvas.Left="12"
252254
Canvas.Top="12"
253255
Width="0"
254256
Height="0"
255-
Fill="{TemplateBinding Foreground}"
257+
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:CheckBoxAssist.CheckBoxForeground)}"
256258
IsHitTestVisible="False"
257259
Opacity="0"
258260
RenderTransformOrigin="0.5,0.5">

src/MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.RadioButton.xaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Setter Property="BorderThickness" Value="1" />
4242
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
4343
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />
44+
<Setter Property="wpf:RadioButtonAssist.RadioButtonForeground" Value="{DynamicResource MaterialDesign.Brush.Primary}"/>
4445
<Setter Property="Template">
4546
<Setter.Value>
4647
<ControlTemplate TargetType="{x:Type RadioButton}">
@@ -81,14 +82,14 @@
8182
<Canvas Width="24" Height="24">
8283
<Path x:Name="Graphic"
8384
Data="M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"
84-
Fill="{DynamicResource MaterialDesign.Brush.RadioButton.Outline}" />
85+
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:RadioButtonAssist.RadioButtonForeground)}" />
8586
<Ellipse x:Name="InteractionEllipse"
8687
Canvas.Left="12"
8788
Canvas.Top="12"
8889
IsHitTestVisible="False"
8990
Width="0"
9091
Height="0"
91-
Fill="{TemplateBinding Foreground}"
92+
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:RadioButtonAssist.RadioButtonForeground)}"
9293
Opacity="0"
9394
RenderTransformOrigin="0.5,0.5">
9495
<Ellipse.RenderTransform>
@@ -126,7 +127,7 @@
126127
<Trigger Property="IsPressed" Value="true" />
127128
<Trigger Property="IsChecked" Value="true">
128129
<Setter TargetName="Graphic" Property="Data" Value="M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M12,7A5,5 0 0,0 7,12A5,5 0 0,0 12,17A5,5 0 0,0 17,12A5,5 0 0,0 12,7Z" />
129-
<Setter TargetName="Graphic" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
130+
<Setter TargetName="Graphic" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:RadioButtonAssist.RadioButtonForeground)}" />
130131
</Trigger>
131132
<Trigger Property="IsChecked" Value="{x:Null}">
132133
<Setter TargetName="Graphic" Property="Data" Value="M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M12,7A5,5 0 0,0 7,12A5,5 0 0,0 12,17A5,5 0 0,0 17,12A5,5 0 0,0 12,7Z" />
@@ -209,14 +210,14 @@
209210
<Canvas Width="24" Height="24">
210211
<Path x:Name="Graphic"
211212
Data="M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"
212-
Fill="{TemplateBinding Foreground}" />
213+
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:RadioButtonAssist.RadioButtonForeground)}" />
213214
<Ellipse x:Name="InteractionEllipse"
214215
Canvas.Left="12"
215216
Canvas.Top="12"
216217
IsHitTestVisible="False"
217218
Width="0"
218219
Height="0"
219-
Fill="{TemplateBinding Foreground}"
220+
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:RadioButtonAssist.RadioButtonForeground)}"
220221
Opacity="0"
221222
RenderTransformOrigin="0.5,0.5">
222223
<Ellipse.RenderTransform>

0 commit comments

Comments
 (0)