惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
有赞技术团队
有赞技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
博客园_首页
Y
Y Combinator Blog
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
P
Proofpoint News Feed
B
Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - zhaotianff

WPF控件样式和模板全解析-ToolTip控件 WPF控件样式和模板全解析-ToggleButton控件 WPF控件样式和模板全解析-CheckBox控件 软件架构风格与软件架构设计方法 WPF控件样式和模板全解析-Label控件 WPF控件样式和模板全解析-Button控件 Windows一直卡在检查更新的解决方案 - zhaotianff - 博客园 AI时代,程序员应该何去何从? C++中的explicit关键字 如何解决Windows 10/11 删除打印机时,拒绝访问的问题 解决IIS服务器typecho上传附件后无法访问的问题 Visual Studio编译过程中拷贝文件的一些实用小技巧 Jenkins快速入门教程 .NET是什么?为什么要选择.NET? 如何在WPF中实现类似DevOps的自动化流程 一文带你搞懂医疗器械设计开发全生命周期 Windows如何清除本机记录的git用户名和密码 HL7协议详解 医疗行业 GDT 数据格式详解 一文带你搞懂C# 异步编程(async/await)底层原理 如何在C#中使用Chromium headless(无头模式)浏览器 不同.NET版本中的WPF新增功能 如何在WPF中使用 Fluent 主题 Windows平台下的各种原生UI框架介绍 C#如何Hook托管函数 WinDbg 用户层调试进阶教程 Windows编程的一些基础理论 推荐一款优秀的Windows经典文件管理器项目-WinFile Windows如何阻止应用程序联网 如何在WPF中捕获窗口外的事件
WPF控件样式和模板全解析-RadioButton控件
zhaotianff · 2026-09-08 · via 博客园 - zhaotianff

RadioButton的默认样式和模板

依旧是使用Blend来查看RadioButton的默认样式和模板

 1  <Style x:Key="FocusVisual">
 2      <Setter Property="Control.Template">
 3          <Setter.Value>
 4              <ControlTemplate>
 5                  <Rectangle Margin="2" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>
 6              </ControlTemplate>
 7          </Setter.Value>
 8      </Setter>
 9  </Style>
10  <Style x:Key="OptionMarkFocusVisual">
11      <Setter Property="Control.Template">
12          <Setter.Value>
13              <ControlTemplate>
14                  <Rectangle Margin="14,0,0,0" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>
15              </ControlTemplate>
16          </Setter.Value>
17      </Setter>
18  </Style>
19  <SolidColorBrush x:Key="RadioButton.Static.Background" Color="#FFFFFFFF"/>
20  <SolidColorBrush x:Key="RadioButton.Static.Border" Color="#FF707070"/>
21  <SolidColorBrush x:Key="RadioButton.Static.Glyph" Color="#FF212121"/>
22  <SolidColorBrush x:Key="RadioButton.MouseOver.Background" Color="#FFF3F9FF"/>
23  <SolidColorBrush x:Key="RadioButton.MouseOver.Border" Color="#FF5593FF"/>
24  <SolidColorBrush x:Key="RadioButton.MouseOver.Glyph" Color="#FF212121"/>
25  <SolidColorBrush x:Key="RadioButton.Pressed.Background" Color="#FFD9ECFF"/>
26  <SolidColorBrush x:Key="RadioButton.Pressed.Border" Color="#FF3C77DD"/>
27  <SolidColorBrush x:Key="RadioButton.Pressed.Glyph" Color="#FF212121"/>
28  <SolidColorBrush x:Key="RadioButton.Disabled.Background" Color="#FFE6E6E6"/>
29  <SolidColorBrush x:Key="RadioButton.Disabled.Border" Color="#FFBCBCBC"/>
30  <SolidColorBrush x:Key="RadioButton.Disabled.Glyph" Color="#FF707070"/>
31  <Style x:Key="RadioButtonStyle1" TargetType="{x:Type RadioButton}">
32      <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
33      <Setter Property="Background" Value="{StaticResource RadioButton.Static.Background}"/>
34      <Setter Property="BorderBrush" Value="{StaticResource RadioButton.Static.Border}"/>
35      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
36      <Setter Property="BorderThickness" Value="1"/>
37      <Setter Property="Template">
38          <Setter.Value>
39              <ControlTemplate TargetType="{x:Type RadioButton}">
40                  <Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
41                      <Grid.ColumnDefinitions>
42                          <ColumnDefinition Width="Auto"/>
43                          <ColumnDefinition Width="*"/>
44                      </Grid.ColumnDefinitions>
45                      <Border x:Name="radioButtonBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="100" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1,1,2,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
46                          <Grid x:Name="markGrid" Margin="2">
47                              <Ellipse x:Name="optionMark" Fill="{StaticResource RadioButton.Static.Glyph}" MinWidth="6" MinHeight="6" Opacity="0"/>
48                          </Grid>
49                      </Border>
50                      <ContentPresenter x:Name="contentPresenter" Grid.Column="1" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
51                  </Grid>
52                  <ControlTemplate.Triggers>
53                      <Trigger Property="HasContent" Value="true">
54                          <Setter Property="FocusVisualStyle" Value="{StaticResource OptionMarkFocusVisual}"/>
55                          <Setter Property="Padding" Value="4,-1,0,0"/>
56                      </Trigger>
57                      <Trigger Property="IsMouseOver" Value="true">
58                          <Setter Property="Background" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.MouseOver.Background}"/>
59                          <Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.MouseOver.Border}"/>
60                          <Setter Property="Fill" TargetName="optionMark" Value="{StaticResource RadioButton.MouseOver.Glyph}"/>
61                      </Trigger>
62                      <Trigger Property="IsEnabled" Value="false">
63                          <Setter Property="Background" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.Disabled.Background}"/>
64                          <Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.Disabled.Border}"/>
65                          <Setter Property="Fill" TargetName="optionMark" Value="{StaticResource RadioButton.Disabled.Glyph}"/>
66                      </Trigger>
67                      <Trigger Property="IsPressed" Value="true">
68                          <Setter Property="Background" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.Pressed.Background}"/>
69                          <Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.Pressed.Border}"/>
70                          <Setter Property="Fill" TargetName="optionMark" Value="{StaticResource RadioButton.Pressed.Glyph}"/>
71                      </Trigger>
72                      <Trigger Property="IsChecked" Value="true">
73                          <Setter Property="Opacity" TargetName="optionMark" Value="1"/>
74                      </Trigger>
75                      <Trigger Property="IsChecked" Value="{x:Null}">
76                          <Setter Property="Opacity" TargetName="optionMark" Value="0.56"/>
77                      </Trigger>
78                  </ControlTemplate.Triggers>
79              </ControlTemplate>
80          </Setter.Value>
81      </Setter>
82  </Style>

拆解RadioButton的样式和模板

VisualState

RadioButton样式示例

示例代码

https://github.com/zhaotianff/cnblog-demo-code/tree/main/WPFStyleAndTemplateDemo