今天同事要在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的更多相关文章

  1. WPF:定制Checkbox样式,让“正确”绿得好看,让“错误”红的显眼

    WPF提供了样式.模板.触发器.状态管理.矢量形状等方式,让我们不需要背景图片,也可以轻松定制控件的风格样式.下面是笔者针对Checkbox进行的样式定制,让“正确”绿得好看,让“错误”红的显眼.  ...

  2. WPF中的CheckBox的_ (underscore / 下划线)丢失

    今天在项目中遇到check box的Content的内容缺少'_', 原因是WPF的ContentPresenter默认会把'_'作为加速键的转义字符.  比方CheckBox的content为&qu ...

  3. radio(单选框)/checkbox(复选框) 美化

    由于某种原因,可能需要对单选框(radio)或复选框(checkbox)进行美化,那么直接修改样式是行不通,要实现就需要添加js,以下js依赖于jquery radio.js: function ra ...

  4. 【WPF】一组CheckBox的全选/全不选功能

    需求:给一组CheckBox做一个全选/全不选的按钮. 思路:CheckBox不像RadioButton那样拥有GroupName属性来分组,于是我想的方法是将这组CheckBox放到一个布局容器中, ...

  5. WPF listbox中Checkbox横向排列

    <ListBox Height="220" Margin="0" ItemsSource="{Binding RightCollection}& ...

  6. RadioButton与CheckBox

    笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧! RadioButton(单选)和CheckBox(多选) 先来看看继承 ...

  7. WPF点滴

    1 设置窗体的最大化,而且无边框 <Style x:Key="WindowsStyle" TargetType="Window"> <Sett ...

  8. WPF Template模版之DataTemplate与ControlTemplate【一】

    WPF Template模版之DataTemplate与ControlTemplate[一] 标签: Wpf模版 2015-04-19 11:52 510人阅读 评论(0) 收藏 举报  分类: -- ...

  9. WPF模板(一)详细介绍

    本次随笔来源于电子书,人家的讲解很好,我就不画蛇添足了. 图形用户界面应用程序较之控制台界面应用程序最大的好处就是界面友好.数据显示直观.CUI程序中数据只能以文本的形式线性显示,GUI程序则允许数据 ...

随机推荐

  1. Java数据结构——栈的应用(以数制转换为例子)

    看一万遍,不如自己动手实践,请不要直接copy代码,先自己去理解例子实现的原理,然后试着自己动手去实践. 用Java去实现栈操作,会用到stack这个类,这类中有几个方法需要知道他们的用法  bool ...

  2. vim 插件之 gist.vim 的安装

    用 IntelliJ 的时觉得 create gist 很好用,查了下,发现 vim 下也有这个插件,于是马上配置上. 安装 下载 Gist.vim 解压后进入目录,拷贝文件 cp plugin/gi ...

  3. phpstorm 10注释的双斜线位置不在缩进的位置:

    22:07 2016/4/4phpstorm 10注释的双斜线位置不在缩进的位置:终于找到了(但是没有实现效果,不知道是什么原因 win10系统):File | Settings | Editor | ...

  4. 【尺取】POJ 3320

    POJ 3320 Jessica's Reading Problem 题意:一本书P页,第i页有ai知识点,问你至少从某一处开始连续要翻多少页才能复习完所有的知识点,不能跨页翻. 思路:<挑战程 ...

  5. Unity Android加密DLL笔记

    unity mono 地址:https://github.com/Unity-Technologies/mono 下载与unity版本对应的unity mono. brew安装:http://brew ...

  6. 【JBOSS】 JBOSS目录结构

    JBOSS在默认情况下可以用3种方式启动minimal,default和all.三种模式内部的模块数量依次递增   例如: 1-执行JBOSS_HOME/bin/run.bat批量处理文件启动JBos ...

  7. Android图书应用-西游记

    下载App 屏幕截图:          功能介绍:1. 侧边滑动目录导航2. 阅读偏好设置3. 记忆阅读进度 内容介绍: 西游记,中国古典名著,内容分三大部分: 第一部分(一到七回)介绍孙悟空的神通 ...

  8. ubuntu下出现的问题-控制台更新源失败

    Ubuntu下控制台输入sudo apt-get update之后出现的问题:E: Could not get lock /var/lib/apt/lists/lock - open (11: Res ...

  9. JavaMail和James的秘密花园

    JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输.我们可以基于JavaMail开发出类似于Micros ...

  10. Struct2 csv文件上传读取中文内容乱码

    网络上搜索下,发现都不适合 最终改写代码: FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br= n ...