Skip to content

add RadioButton and CheckBox custom foreground #3773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6564c4c
RadioButton 添加自定义大小功能
mzy666888 Nov 20, 2024
23b57da
Adding sample to normal WPF demo app as well
Keboo Nov 21, 2024
403c937
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Nov 21, 2024
e6c4e5a
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Nov 26, 2024
e237a9c
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Dec 17, 2024
db466c2
the MaterialDesignUserForegroundCheckBox style for custom checkboxsize
mzy666888 Dec 17, 2024
2b780d2
Merge branch 'master' of https://github.com/mzy666888/MaterialDesignI…
mzy666888 Dec 17, 2024
a7eb51e
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Dec 19, 2024
bac3b9e
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Jan 2, 2025
0f3edba
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Jan 6, 2025
1f7dd72
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Jan 8, 2025
71a46b1
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Jan 14, 2025
68cf48b
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Jan 15, 2025
6dff589
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Jan 21, 2025
a9beaaa
RadioButton and CheckBox use custom foreground
mzy666888 Jan 21, 2025
eaf5c0b
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Feb 1, 2025
2fef739
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Feb 18, 2025
58e0af4
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Feb 24, 2025
b66043a
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Mar 18, 2025
23e1a3e
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Mar 27, 2025
b344e17
Merge branch 'MaterialDesignInXAML:master' into master
mzy666888 Apr 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/MaterialDesign3.Demo.Wpf/Toggles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@
<smtx:XamlDisplay Margin="0,5,8,8"
VerticalAlignment="Center"
UniqueKey="buttons_42221">
<RadioButton Content="Custom RadioButton Size" materialDesign:RadioButtonAssist.RadioButtonSize="40" Style="{StaticResource MaterialDesignRadioButton}" />
<RadioButton Content="Custom RadioButton Size and foreground" materialDesign:RadioButtonAssist.RadioButtonSize="40" IsChecked="False" materialDesign:RadioButtonAssist.RadioButtonForeground="Blue" />
</smtx:XamlDisplay>
<smtx:XamlDisplay Margin="0,5,8,8"
VerticalAlignment="Center"
UniqueKey="buttons_42222">
<RadioButton Content="Custom RadioButton Size and foreground for UserStyle" materialDesign:RadioButtonAssist.RadioButtonSize="30" IsChecked="True" Style="{DynamicResource MaterialDesignUserForegroundRadioButton}" materialDesign:RadioButtonAssist.RadioButtonForeground="Blue" />
</smtx:XamlDisplay>

<smtx:XamlDisplay Margin="0,5,8,8"
Expand Down Expand Up @@ -425,9 +430,12 @@
VerticalAlignment="Top"
UniqueKey="checkboxes_2">
<StackPanel Margin="8,0">
<CheckBox materialDesign:CheckBoxAssist.CheckBoxSize="30"
<CheckBox materialDesign:CheckBoxAssist.CheckBoxSize="30"
Content="Custom Size"
IsChecked="True" />
<CheckBox materialDesign:CheckBoxAssist.CheckBoxSize="30" materialDesign:CheckBoxAssist.CheckBoxForeground="Red"
Content="Custom Size and foreground" IsChecked="True"
/>
<CheckBox materialDesign:RippleAssist.IsDisabled="True"
Content="Ripple disabled"
IsChecked="True" />
Expand Down
27 changes: 25 additions & 2 deletions src/MaterialDesignThemes.Wpf/CheckBoxAssist.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
using System.Windows.Media;

namespace MaterialDesignThemes.Wpf;

public class CheckBoxAssist
{
private const double DefaultCheckBoxSize = 18.0;

#region AttachedProperty : CheckBoxSizeProperty
public static readonly DependencyProperty CheckBoxSizeProperty
= DependencyProperty.RegisterAttached("CheckBoxSize", typeof(double), typeof(CheckBoxAssist), new PropertyMetadata(DefaultCheckBoxSize));
public static readonly DependencyProperty CheckBoxSizeProperty = DependencyProperty.RegisterAttached(
"CheckBoxSize",
typeof(double),
typeof(CheckBoxAssist),
new PropertyMetadata(DefaultCheckBoxSize)
);

public static double GetCheckBoxSize(CheckBox element) => (double)element.GetValue(CheckBoxSizeProperty);

public static void SetCheckBoxSize(CheckBox element, double checkBoxSize) => element.SetValue(CheckBoxSizeProperty, checkBoxSize);
#endregion



#region AttachedProperty : CheckBoxForegroundProperty
public static readonly DependencyProperty CheckBoxForegroundProperty = DependencyProperty.RegisterAttached(
"CheckBoxForeground",
typeof(Brush),
typeof(CheckBoxAssist),
null
);

public static Brush GetCheckBoxForeground(CheckBox element) => (Brush)element.GetValue(CheckBoxForegroundProperty);

public static void SetCheckBoxForeground(CheckBox element, Brush checkBoxForeground) =>
element.SetValue(CheckBoxForegroundProperty, checkBoxForeground);
#endregion
}
38 changes: 26 additions & 12 deletions src/MaterialDesignThemes.Wpf/RadioButtonAssist.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
namespace MaterialDesignThemes.Wpf
using System.Windows.Media;

namespace MaterialDesignThemes.Wpf
{
public class RadioButtonAssist
{
private const double DefaultRadioButtonSize = 18.0;

#region AttachedProperty : RadioButtonSizeProperty
public static readonly DependencyProperty RadioButtonSizeProperty =
DependencyProperty.RegisterAttached(
"RadioButtonSize",
typeof(double),
typeof(RadioButtonAssist),
new PropertyMetadata(DefaultRadioButtonSize)
);
public static readonly DependencyProperty RadioButtonSizeProperty = DependencyProperty.RegisterAttached(
"RadioButtonSize",
typeof(double),
typeof(RadioButtonAssist),
new PropertyMetadata(DefaultRadioButtonSize)
);

public static double GetRadioButtonSize(RadioButton element) => (double)element.GetValue(RadioButtonSizeProperty);

public static void SetRadioButtonSize(RadioButton element, double RadioButtonSize) =>
element.SetValue(RadioButtonSizeProperty, RadioButtonSize);
#endregion

#region AttachedProperty :RadioButtonForegroundProperty
public static readonly DependencyProperty RadioButtonForegroundProperty = DependencyProperty.RegisterAttached(
"RadioButtonForeground",
typeof(Brush),
typeof(RadioButtonAssist),
null
);

public static double GetRadioButtonSize(RadioButton element) =>
(double)element.GetValue(RadioButtonSizeProperty);
public static Brush GetRadioButtonForeground(RadioButton element) => (Brush)element.GetValue(RadioButtonForegroundProperty);

public static void SetRadioButtonSize(RadioButton element, double checkBoxSize) =>
element.SetValue(RadioButtonSizeProperty, checkBoxSize);
public static void SetRadioButtonForeground(RadioButton element, Brush radioButtonForeground) =>
element.SetValue(RadioButtonForegroundProperty, radioButtonForeground);
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Setter Property="BorderThickness" Value="1" />
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />
<Setter Property="wpf:CheckBoxAssist.CheckBoxForeground" Value="{DynamicResource MaterialDesign.Brush.Primary}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
Expand Down Expand Up @@ -111,13 +112,13 @@
<Canvas Width="24" Height="24">
<Path x:Name="Graphic"
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"
Fill="{DynamicResource MaterialDesign.Brush.ForegroundLight}" />
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:CheckBoxAssist.CheckBoxForeground)}" />
<Ellipse x:Name="InteractionEllipse"
Canvas.Left="12"
Canvas.Top="12"
Width="0"
Height="0"
Fill="{TemplateBinding Foreground}"
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:CheckBoxAssist.CheckBoxForeground)}"
IsHitTestVisible="False"
Opacity="0"
RenderTransformOrigin="0.5,0.5">
Expand Down Expand Up @@ -158,7 +159,7 @@
<Trigger Property="IsPressed" Value="true" />
<Trigger Property="IsChecked" Value="true">
<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" />
<Setter TargetName="Graphic" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
<Setter TargetName="Graphic" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:CheckBoxAssist.CheckBoxForeground)}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" Value="0.56" />
Expand Down Expand Up @@ -205,6 +206,7 @@
<Setter Property="BorderThickness" Value="1" />
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />
<Setter Property="wpf:CheckBoxAssist.CheckBoxForeground" Value="{DynamicResource MaterialDesign.Brush.Primary}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
Expand Down Expand Up @@ -246,13 +248,13 @@
<Canvas Width="24" Height="24">
<Path x:Name="Graphic"
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"
Fill="{TemplateBinding Foreground}" />
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:CheckBoxAssist.CheckBoxForeground)}" />
<Ellipse x:Name="InteractionEllipse"
Canvas.Left="12"
Canvas.Top="12"
Width="0"
Height="0"
Fill="{TemplateBinding Foreground}"
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:CheckBoxAssist.CheckBoxForeground)}"
IsHitTestVisible="False"
Opacity="0"
RenderTransformOrigin="0.5,0.5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Setter Property="BorderThickness" Value="1" />
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}" />
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />
<Setter Property="wpf:RadioButtonAssist.RadioButtonForeground" Value="{DynamicResource MaterialDesign.Brush.Primary}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
Expand Down Expand Up @@ -81,14 +82,14 @@
<Canvas Width="24" Height="24">
<Path x:Name="Graphic"
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"
Fill="{DynamicResource MaterialDesign.Brush.RadioButton.Outline}" />
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:RadioButtonAssist.RadioButtonForeground)}" />
<Ellipse x:Name="InteractionEllipse"
Canvas.Left="12"
Canvas.Top="12"
IsHitTestVisible="False"
Width="0"
Height="0"
Fill="{TemplateBinding Foreground}"
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:RadioButtonAssist.RadioButtonForeground)}"
Opacity="0"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
Expand Down Expand Up @@ -126,7 +127,7 @@
<Trigger Property="IsPressed" Value="true" />
<Trigger Property="IsChecked" Value="true">
<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" />
<Setter TargetName="Graphic" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
<Setter TargetName="Graphic" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:RadioButtonAssist.RadioButtonForeground)}" />
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<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" />
Expand Down Expand Up @@ -209,14 +210,14 @@
<Canvas Width="24" Height="24">
<Path x:Name="Graphic"
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"
Fill="{TemplateBinding Foreground}" />
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:RadioButtonAssist.RadioButtonForeground)}" />
<Ellipse x:Name="InteractionEllipse"
Canvas.Left="12"
Canvas.Top="12"
IsHitTestVisible="False"
Width="0"
Height="0"
Fill="{TemplateBinding Foreground}"
Fill="{Binding RelativeSource={RelativeSource TemplatedParent} ,Path=(wpf:RadioButtonAssist.RadioButtonForeground)}"
Opacity="0"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
Expand Down