利用radioButton的groupName分组互斥。。再解决radiobutton的取消选择的问题。给radiobutton加了一个像checkbox的样式

2个方式:

效果图

第一种usecontrol:

xaml:

View Code

<RadioButton x:Class="GEMS.Windows.Controls.UserControls.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" Click="RadioButton_Click_1" Unchecked="RadioButton_Unchecked_1" Style="{DynamicResource SingleCheckBox}"><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" /><!--<Path Width="14" Height="11" Margin="5,2,0,0" x:Name="CheckMark" SnapsToDevicePixels="False" Stroke="{StaticResource Glyphbrush}" StrokeThickness="2" Data="M 0 0 L 7 7 M 0 7 L 7 0" /> cross--></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>

xaml的后台cs

View Code

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 GEMS.Windows.Controls.UserControls
{/// <summary>/// Interaction logic for SingleCheckBox.xaml/// </summary>public partial class SingleCheckBox : RadioButton{private bool hasCheck;/// <summary>/// /// </summary>public bool HasCheck{get { return hasCheck; }set { hasCheck = value; }}public SingleCheckBox(){InitializeComponent();}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;}}
}

第2种customercontrol

注意的是在generic里面需要去引用singleCheckBox.xaml

View Code

<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/GEMS.Windows.Controls.CustomControls;component/Themes/CustomRichTextBox.xaml"/><ResourceDictionary Source="/GEMS.Windows.Controls.CustomControls;component/Themes/ColorPicker.xaml"/><ResourceDictionary Source="/GEMS.Windows.Controls.CustomControls;component/Themes/MultipleDropDownBox.xaml"/><ResourceDictionary Source="/GEMS.Windows.Controls.CustomControls;component/Themes/SingleCheckBox.xaml"/></ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

在singleCheckBox.xaml中定义样式

View Code

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:GEMS.Windows.Controls.CustomControls"><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  TargetType="{x:Type local:SingleCheckBox}"><Setter Property="GroupName" Value="Single"/><Setter Property="SnapsToDevicePixels" Value="true"/><Setter Property="Foreground" Value="#071f3b"/><Setter Property="FontFamily" Value="Arial"></Setter><Setter Property="FontSize" Value="14"></Setter><Setter Property="Background" Value="Red"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:SingleCheckBox}"><BulletDecorator Background="Transparent"><BulletDecorator.Bullet><Border x:Name="Border"  Width="20" Height="20" CornerRadius="4" Background="#ffffff" BorderThickness="1" BorderBrush="Black"><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>
</ResourceDictionary>

在singleCheckBox.cs中
注意为了通知wpf正在提供一个信阳市,需要在自定义控件类的静态构造中调用overrideMetadata()。。我们使用DefaultStyleKeyProperty来调用这个方式

static SingleCheckBox()

{

DefaultStyleKeyProperty.OverrideMetadata(

typeof(SingleCheckBox), newFrameworkPropertyMetadata(typeof(SingleCheckBox

)));

}

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;namespace GEMS.Windows.Controls.CustomControls
{public  class SingleCheckBox:RadioButton{private bool hasCheck;/// <summary>/// /// </summary>public bool HasCheck{get { return hasCheck; }set { hasCheck = value; }}public SingleCheckBox(){this.Unchecked += SingleCheckBox_Unchecked;this.Click += SingleCheckBox_Click;}void SingleCheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e){this.HasCheck = false;}static SingleCheckBox(){DefaultStyleKeyProperty.OverrideMetadata(typeof(SingleCheckBox), new FrameworkPropertyMetadata(typeof(SingleCheckBox)));//
      }void SingleCheckBox_Click(object sender, System.Windows.RoutedEventArgs e){if (this.HasCheck == false){this.HasCheck = true;this.IsChecked = true;}else{this.HasCheck = false;this.IsChecked = false;}}}
}

转载于:https://www.cnblogs.com/FaDeKongJian/archive/2013/02/21/2920416.html

wpf 可以取消的单选checkbox相关推荐

  1. WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式

    原文:WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式 一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等, ...

  2. 全选或者单选checkbox的值动态添加到div

    图片.png <!DOCTYPE html> <html><head><meta charset="utf-8" /><tit ...

  3. WPF 基础控件之CheckBox样式

    WPF开发者QQ群:340500857 由于微信群人数太多入群请添加小编微信号  yanjinhuawechat 或 W_Feng_aiQ 邀请入群 需备注WPF开发者 PS:有更好的方式欢迎推荐. ...

  4. checkbox wpf 背景图片_WPF的CheckBox样式总结

    背景 很多时候我们使用WPF开发界面的时候经常会用到各种空间,很多时候我们需要去自定义控件的样式来替换默认的样式,今天通过两个方法来替换WPF中的CheckBox样式,透过这两个例子我们可以掌握基本的 ...

  5. 转:只能选择GridView中的一个CheckBox(单选CheckBox)

    方法1: protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { CheckBox cbx = e ...

  6. php单选框点击取消,取消选中单选框radio的三种方式

    本文提供了三种取消选中radio的方式,代码示例如下: 本文依赖于jQuery,其中第一种,第二种方式是使用jQuery实现的,第三种方式是基于JS和DOM实现的. HTML> 单选按钮取消选中 ...

  7. 取消js单选框的小圆圈

    input[type=checkbox], input[type=radio] { opacity: 0; }

  8. 取消选中单选框radio的三种方式

    作者: 铁锚 日期: 2013年12月21日 本文提供了三种取消选中radio的方式,代码示例如下: 本文依赖于jQuery,其中第一种,第二种方式是使用jQuery实现的,第三种方式是基于JS和DO ...

  9. radio单选框的选中与取消(超详细讲解!)

    一.前情提要 刚开始想做的效果是这样:我操作了checkbox复选框,radio单选框的状态会随之变化.进而引出一系列问题,如:1.radio单选框选中了如何取消,取消了如何再选中:2.当checkb ...

最新文章

  1. 参数化查询 但未提供该参数(将null插入数据库)
  2. Aspose转PDF时乱码问题的解决
  3. 《剑指Offer》60:n个骰子的点数
  4. 网站服务器需要定期重启吗,数据库服务器需要定期重启吗
  5. 手把手自己造无人驾驶车,是什么样的体验?
  6. 活动地推怎么做最有效?
  7. python GUI打开文本文件代码
  8. c语言 iostream,C语言 我应该在哪里使用iostream类?
  9. 逆向破解必备基础smail基础语法
  10. 左神算法:找到二叉树中的最大搜索二叉子树(树形dp套路,Java版)
  11. BDBR和BD-PSNR
  12. 获取网页视频,日常下载工具推荐——XDM
  13. MFC CString转ASCII字符串
  14. 宝可梦 图片识别python_初探利用Python进行图文识别(OCR)
  15. docker中vim无法粘贴
  16. 7-23 币值转换 (20 分)
  17. 消息称Snapchat将收购自制表情应用开发商Bitstrips
  18. API工具栏教你如何采集淘宝拼多多商品详情数据
  19. 忙忙碌碌一整天,躺下总是老失眠,要想摆脱此状态,赶快听经解心宽,哈哈
  20. 大数据培训技术 Cube和Cuboid

热门文章

  1. 如何使用Squid服务来构建=》传统和透明代理服务器,通俗易懂!
  2. Linux操作系统基础解析之(五)——grep命令家族及正则表达式初识
  3. [译]基于GPU的体渲染高级技术之raycasting算法
  4. three.js插件实现立体动感视频播放效果
  5. Spring入门5.事务管理机制
  6. 通过jQuery源码学习javascript(三)
  7. 【转帖】Reporting Service rdl报表,在aspx页面显示一张完整的RDL报表
  8. DataSet与Xml之间的转换
  9. 一种在未来互联网中的面向用户的云操作系统体系
  10. 《Java程序员全攻略:从小工到专家》连载八:加入什么样的公司