【转】WPF 单选的Checkbox
今天同事要在DataGrid里用单选的Checkbox,我感觉很多余,因为正常DataGrid就可以单选,为什么还要加一列Checkbox,但是人家要求再那里,我就告诉他,可以用RadioButton,然后写个Checkbox的样式就可以了。
因为本人不太会写样式,因此在网上搜到了前辈的一篇帖子,拿来应用,效果着实不错,感谢法的空间大神
RadioButton页面的XAML代码
<RadioButton x:Class="CheckBoxRadioButton.SingleCheckBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Style="{DynamicResource SingleCheckBox}" Click="RadioButton_Click_1" Unchecked="RadioButton_Unchecked_1">
<RadioButton.Resources>
<Style x:Key="RadioButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border>
<Rectangle
Margin="15,0,0,0"
StrokeThickness="1"
Stroke="#60000000"
StrokeDashArray="1 2"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
<SolidColorBrush x:Key="Normalborderbrush" Color="#5d7fad" />
<Style x:Key="SingleCheckBox" TargetType="{x:Type RadioButton}">
<Setter Property="GroupName" Value="Single"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Foreground" Value="#071f3b"/>
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="FontSize" Value="14"></Setter>
<Setter Property="FocusVisualStyle" Value="{StaticResource RadioButtonFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Border x:Name="Border" Width="20" Height="20" CornerRadius="4" Background="#ffffff" BorderThickness="1" BorderBrush="{StaticResource Normalborderbrush}">
<Path Width="14" Height="11" Margin="5,2,0,0" x:Name="CheckMark" SnapsToDevicePixels="False" Stroke="#173e78" StrokeThickness="2" Data="M 0 5 L 3 10 10 0" />
</Border>
</BulletDecorator.Bullet>
<ContentPresenter
Margin="4,0,0,0"
VerticalAlignment="Center"
HorizontalAlignment="Left"
RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="false">
<Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="CheckMark" Property="Data" Value="M 0 7 L 7 0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="#ffffff" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border" Property="Background" Value="#ffffff" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</RadioButton.Resources>
</RadioButton>
RadioButton页面的CS代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace CheckBoxRadioButton
{
/// <summary>
/// SingleCheckBox.xaml 的交互逻辑
/// </summary>
public partial class SingleCheckBox : RadioButton
{
public SingleCheckBox()
{
InitializeComponent();
} private bool hasCheck;
public bool HasCheck
{
get { return hasCheck; }
set { hasCheck = value; }
} private void RadioButton_Click_1(object sender, RoutedEventArgs e)
{
if (this.HasCheck == false)
{
this.HasCheck = true;
this.IsChecked = true;
}
else
{
this.HasCheck = false;
this.IsChecked = false;
}
} private void RadioButton_Unchecked_1(object sender, RoutedEventArgs e)
{
this.HasCheck = false;
}
}
}
主页面调用
<Window x:Class="CheckBoxRadioButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CheckBoxRadioButton"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Vertical">
<local:SingleCheckBox Content="我是一"/>
<local:SingleCheckBox Content="我是二"/>
</StackPanel>
</Grid>
</Window>
效果


【转】WPF 单选的Checkbox的更多相关文章
- WPF:定制Checkbox样式,让“正确”绿得好看,让“错误”红的显眼
WPF提供了样式.模板.触发器.状态管理.矢量形状等方式,让我们不需要背景图片,也可以轻松定制控件的风格样式.下面是笔者针对Checkbox进行的样式定制,让“正确”绿得好看,让“错误”红的显眼. ...
- WPF中的CheckBox的_ (underscore / 下划线)丢失
今天在项目中遇到check box的Content的内容缺少'_', 原因是WPF的ContentPresenter默认会把'_'作为加速键的转义字符. 比方CheckBox的content为&qu ...
- radio(单选框)/checkbox(复选框) 美化
由于某种原因,可能需要对单选框(radio)或复选框(checkbox)进行美化,那么直接修改样式是行不通,要实现就需要添加js,以下js依赖于jquery radio.js: function ra ...
- 【WPF】一组CheckBox的全选/全不选功能
需求:给一组CheckBox做一个全选/全不选的按钮. 思路:CheckBox不像RadioButton那样拥有GroupName属性来分组,于是我想的方法是将这组CheckBox放到一个布局容器中, ...
- WPF listbox中Checkbox横向排列
<ListBox Height="220" Margin="0" ItemsSource="{Binding RightCollection}& ...
- RadioButton与CheckBox
笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧! RadioButton(单选)和CheckBox(多选) 先来看看继承 ...
- WPF点滴
1 设置窗体的最大化,而且无边框 <Style x:Key="WindowsStyle" TargetType="Window"> <Sett ...
- WPF Template模版之DataTemplate与ControlTemplate【一】
WPF Template模版之DataTemplate与ControlTemplate[一] 标签: Wpf模版 2015-04-19 11:52 510人阅读 评论(0) 收藏 举报 分类: -- ...
- WPF模板(一)详细介绍
本次随笔来源于电子书,人家的讲解很好,我就不画蛇添足了. 图形用户界面应用程序较之控制台界面应用程序最大的好处就是界面友好.数据显示直观.CUI程序中数据只能以文本的形式线性显示,GUI程序则允许数据 ...
随机推荐
- The World's Only Advanced Operating System
The World's Only Advanced Operating System
- c#文本转语音以及语音阅读小实例
c#实现语音阅读以及文本转语音文件是基于c#的一个类库(SpeechSynthesizer )实现的,使用该类必须要添加引用using System.Speech.Synthesis;直接是无法添加引 ...
- Torch Problems: require some packages doesn't work
I've recently got a problem. require 'cutorch' doesn't work. But it was ok yesterday, although I hav ...
- VS调试程序时一闪而过的问题-解决方法(网上搜集)
在VS2012里的控制台应用程序在运行时,结果画面一闪而过,不管是用F5 还是用Ctrl + F5都是一样,导致无法看到结果. 网上有不少的办法,说是都是在程序最后加一个要程序暂停的语句或从控制台上获 ...
- RFID工作流程
读写器通过发射天线发送一定频率的射频信号,² 当射频卡进入发射天线工作区域时产生感应电²流,射频卡获得能量被启动.²²射频卡将自身编码等信息透过卡内天线发送出²去.²²读写器接收天线接收到从 射频卡发 ...
- matlab 中 eps 的分析
eps(a)是|a|与大于|a|的最小的浮点数之间的距离,距离越小表示精度越高.默认a=1: 这里直接在matlab中输入:eps == eps(1)(true). 我们知道浮点数其实是离散的,有限的 ...
- 在.NET中使用反射实现简易插件机制
本篇是我学习反射的一个应用小场景而做的学习笔记,主要是一个小的总结,并对各个步骤的记录,以便将来回顾. 一.基础框架-敏捷基础版本 这里假定我们要开发一个记事本,选择Windows Form技术开发, ...
- C# Random生成多个不重复的随机数万能接口
C#,Radom.Next()提供了在一定范围生成一个随机数的方法,我现在有个业务场景是给其他部门推送一些数据供他们做抽样检查处理,假设我的数据库里面有N条数据,现在要定期给其随机推送数据,我需要先拿 ...
- 【Java并发编程实战】-----“J.U.C”:Condition
在看Condition之前,我们先来看下面这个例子: 工厂类,用来存放.取出商品: public class Depot { private int depotSize; //仓库大小 private ...
- Alljoyn瘦客户端库介绍(官方文档翻译 下)
由于其他事情耽误,这个翻译现在才完成.接上篇—— 4 瘦客户端核心库架构 由于AllJoyn瘦客户端核心库(AJTCL)必须运行在那些功耗受限.计算能力有限.资源紧缺的设备上,因此它无法像运行在通用型 ...