WPF 把CheckBox渲染成圆形的

<CheckBox Background="Red">

<CheckBox.Resources>

<Style x:Key="OptionMarkFocusVisual">

<Setter Property="Control.Template">

<Setter.Value>

<ControlTemplate>

<Rectangle Margin="14,0,0,0" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

<SolidColorBrush x:Key="OptionMark.Static.Glyph" Color="#FF212121"/>

<SolidColorBrush x:Key="OptionMark.MouseOver.Background" Color="#FFF3F9FF"/>

<SolidColorBrush x:Key="OptionMark.MouseOver.Border" Color="#FF5593FF"/>

<SolidColorBrush x:Key="OptionMark.MouseOver.Glyph" Color="#FF212121"/>

<SolidColorBrush x:Key="OptionMark.Pressed.Background" Color="#FFD9ECFF"/>

<SolidColorBrush x:Key="OptionMark.Pressed.Border" Color="#FF3C77DD"/>

<SolidColorBrush x:Key="OptionMark.Pressed.Glyph" Color="#FF212121"/>

<SolidColorBrush x:Key="OptionMark.Disabled.Background" Color="#FFE6E6E6"/>

<SolidColorBrush x:Key="OptionMark.Disabled.Border" Color="#FFBCBCBC"/>

<SolidColorBrush x:Key="OptionMark.Disabled.Glyph" Color="#FF707070"/>

<Style TargetType="CheckBox">

<Setter Property="Width" Value="28"/>

<Setter Property="Height" Value="28"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type CheckBox}">

<Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto"/>

<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

<Border x:Name="checkBoxBorder" Background="{TemplateBinding Background}" CornerRadius="15" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">

<Grid x:Name="markGrid">

<Path x:Name="optionMark" Data="F1 M 9.97498,1.22334L 4.6983,9.09834L 4.52164,9.09834L 0,5.19331L 1.27664,3.52165L 4.255,6.08833L 8.33331,1.52588e-005L 9.97498,1.22334 Z " Fill="{StaticResource OptionMark.Static.Glyph}" Margin="1" Opacity="0" Stretch="None"/>

<Rectangle x:Name="indeterminateMark" Fill="{StaticResource OptionMark.Static.Glyph}" Margin="2" Opacity="0"/>

</Grid>

</Border>

<ContentPresenter x:Name="contentPresenter" Grid.Column="1" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

</Grid>

<ControlTemplate.Triggers>

<Trigger Property="HasContent" Value="true">

<Setter Property="FocusVisualStyle" Value="{StaticResource OptionMarkFocusVisual}"/>

<Setter Property="Padding" Value="4,-1,0,0"/>

</Trigger>

<Trigger Property="IsMouseOver" Value="true">

<Setter Property="Background" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.MouseOver.Background}"/>

<Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.MouseOver.Border}"/>

<Setter Property="Fill" TargetName="optionMark" Value="{StaticResource OptionMark.MouseOver.Glyph}"/>

<Setter Property="Fill" TargetName="indeterminateMark" Value="{StaticResource OptionMark.MouseOver.Glyph}"/>

</Trigger>

<Trigger Property="IsEnabled" Value="false">

<Setter Property="Background" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.Disabled.Background}"/>

<Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.Disabled.Border}"/>

<Setter Property="Fill" TargetName="optionMark" Value="{StaticResource OptionMark.Disabled.Glyph}"/>

<Setter Property="Fill" TargetName="indeterminateMark" Value="{StaticResource OptionMark.Disabled.Glyph}"/>

</Trigger>

<Trigger Property="IsPressed" Value="true">

<Setter Property="Background" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.Pressed.Background}"/>

<Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="{StaticResource OptionMark.Pressed.Border}"/>

<Setter Property="Fill" TargetName="optionMark" Value="{StaticResource OptionMark.Pressed.Glyph}"/>

<Setter Property="Fill" TargetName="indeterminateMark" Value="{StaticResource OptionMark.Pressed.Glyph}"/>

</Trigger>

<Trigger Property="IsChecked" Value="true">

<Setter Property="Opacity" TargetName="optionMark" Value="1"/>

<Setter Property="Opacity" TargetName="indeterminateMark" Value="0"/>

</Trigger>

<Trigger Property="IsChecked" Value="{x:Null}">

<Setter Property="Opacity" TargetName="optionMark" Value="0"/>

<Setter Property="Opacity" TargetName="indeterminateMark" Value="1"/>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</CheckBox.Resources>

</CheckBox>

参考:使用WPF将复选框从方形更改为圆形 _大数据知识库

相关推荐
九鼎科技-Leo14 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
麻花20131 天前
C#之WPF的C1FlexGrid空间的行加载事件和列事件变更处理动态加载的枚举值
开发语言·c#·wpf
lcintj1 天前
【WPF】Prism学习(九)
学习·wpf·prism
界面开发小八哥1 天前
界面控件DevExpress WPF中文教程:网格视图数据布局的列和卡片字段
wpf·界面控件·devexpress·ui开发·用户界面
△曉風殘月〆1 天前
如何在WPF中嵌入其它程序
wpf
Crazy Struggle1 天前
功能齐全的 WPF 自定义控件资源库(收藏版)
.net·wpf·ui控件库
shepherd枸杞泡茶2 天前
WPF动画
c#·.net·wpf
lcintj2 天前
【WPF】Prism学习(十)
学习·wpf·prism
wyh要好好学习2 天前
WPF数据加载时添加进度条
ui·wpf
code_shenbing2 天前
跨平台WPF框架Avalonia教程 三
前端·microsoft·ui·c#·wpf·跨平台·界面设计