RadioButton改写的开关按钮
先上效果图:
这个控件其实是俩个RadioButton,通过样式就可以实现。
样式资源:
1 <Style x:Key="Tong_Yong_RadioButtonStyle" TargetType="{x:Type RadioButton}"> 2 <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> 3 <Setter Property="Background" Value="White" /> 4 <Setter Property="BorderThickness" Value="0" /> 5 <Setter Property="HorizontalContentAlignment" Value="Center" /> 6 <Setter Property="VerticalContentAlignment" Value="Center" /> 7 <Setter Property="Padding" Value="0" /> 8 <Setter Property="Margin" Value="10,10,0,0" /> 9 <Setter Property="Cursor" Value="Hand" /> 10 <Setter Property="Height" Value="{Binding Height}" /> 11 <Setter Property="Width" Value="{Binding Width}" /> 12 <Setter Property="Cursor" Value="Hand" /> 13 <Setter Property="Template"> 14 <Setter.Value> 15 <ControlTemplate TargetType="{x:Type RadioButton}"> 16 <Grid> 17 <!-- 选中或者鼠标移入颜色 --> 18 <Grid 19 x:Name="B" 20 Background="#0099ff" 21 Visibility="Collapsed" /> 22 <!-- 默认颜色 --> 23 <Grid x:Name="A" Background="{TemplateBinding Background}" /> 24 25 <ContentPresenter 26 x:Name="b" 27 HorizontalAlignment="Center" 28 VerticalAlignment="Center" 29 TextBlock.Foreground="White" /> 30 <ContentPresenter 31 x:Name="a" 32 HorizontalAlignment="Center" 33 VerticalAlignment="Center" 34 TextBlock.Foreground="Black" /> 35 </Grid> 36 <ControlTemplate.Triggers> 37 <Trigger Property="IsChecked" Value="True"> 38 <Setter TargetName="A" Property="Visibility" Value="Collapsed" /> 39 <Setter TargetName="B" Property="Visibility" Value="Visible" /> 40 <Setter TargetName="a" Property="Visibility" Value="Collapsed" /> 41 <Setter TargetName="b" Property="Visibility" Value="Visible" /> 42 </Trigger> 43 </ControlTemplate.Triggers> 44 </ControlTemplate> 45 </Setter.Value> 46 </Setter> 47 </Style>
2.引用样式
<RadioButton Width="80" Margin="0" Content="开" Foreground="Black" Style="{DynamicResource Tong_Yong_RadioButtonStyle}" /> <RadioButton Width="80" Margin="0" Content="关" Foreground="Black" IsChecked="True" Style="{DynamicResource Tong_Yong_RadioButtonStyle}" />
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器