RadioButton改写的开关按钮
2024-09-06 18:25:27
先上效果图:
这个控件其实是俩个RadioButton,通过样式就可以实现。
样式资源:
<Style x:Key="Tong_Yong_RadioButtonStyle" TargetType="{x:Type RadioButton}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="Background" Value="White" />
<Setter Property="BorderThickness" Value="" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="" />
<Setter Property="Margin" Value="10,10,0,0" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Height" Value="{Binding Height}" />
<Setter Property="Width" Value="{Binding Width}" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid>
<!-- 选中或者鼠标移入颜色 -->
<Grid
x:Name="B"
Background="#0099ff"
Visibility="Collapsed" />
<!-- 默认颜色 -->
<Grid x:Name="A" Background="{TemplateBinding Background}" /> <ContentPresenter
x:Name="b"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextBlock.Foreground="White" />
<ContentPresenter
x:Name="a"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextBlock.Foreground="Black" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="A" Property="Visibility" Value="Collapsed" />
<Setter TargetName="B" Property="Visibility" Value="Visible" />
<Setter TargetName="a" Property="Visibility" Value="Collapsed" />
<Setter TargetName="b" Property="Visibility" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
2.引用样式
<RadioButton
Width=""
Margin=""
Content="开"
Foreground="Black"
Style="{DynamicResource Tong_Yong_RadioButtonStyle}" />
<RadioButton
Width=""
Margin=""
Content="关"
Foreground="Black"
IsChecked="True"
Style="{DynamicResource Tong_Yong_RadioButtonStyle}" />
最新文章
- Android中使用ViewPager实现屏幕页面切换和页面切换效果
- java三大框架
- js实现div层缓慢收缩与展开的方法
- DragLayout: QQ5.0侧拉菜单的新特效
- Python练习题 027:对10个数字进行排序
- AVR ATMEGA8 串口USART
- 不关闭seLinux解决vsftpd服务本地用户不能登录问题(500 OOPS: cannot change directory:/home/***
- 运行yum报错:Error: Cannot retrieve metalink for repository: epel. Please verify its path
- 安装 SQL Server2008 安装程序规则支持提示“重新启动计算机”失败
- 浅谈 js 数字格式类型
- iOS获取设备唯一标识的各种方法?IDFA、IDFV、UDID分别是什么含义?
- Java之StringBuffer,StringBuilder,Math,Date,SimpleDateFormat,UUID,File
- Ubuntu 12.04 LTS 查看网关地址方法汇总
- 安装FrameWork后重新注册IIS
- odoo开发笔记--form视图自定义
- 【mongo】登陆报错
- python+requests接口自动化完整项目设计源码
- java se 随机数。生成
- 查找第K小的元素(利用中位数线性时间选择)(C)
- 20145330 《网络攻防》 MSF基础应用