你的位置:首页 > ASP.net教程

[ASP.net教程]【转】WPF 单选的Checkbox


今天同事要在DataGrid里用单选的Checkbox,我感觉很多余,因为正常DataGrid就可以单选,为什么还要加一列Checkbox,但是人家要求再那里,我就告诉他,可以用RadioButton,然后写个Checkbox的样式就可以了。

因为本人不太会写样式,因此在网上搜到了前辈的一篇帖子,拿来应用,效果着实不错,感谢法的空间大神

RadioButton页面的XAML代码

<RadioButton x:Class="CheckBoxRadioButton.SingleCheckBox"       ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       ="http://schemas.microsoft.com/winfx/2006/xaml"       ="http://schemas.open       ="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"    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    ="http://schemas.microsoft.com/winfx/2006/xaml"     ="clr-namespace:CheckBoxRadioButton"    Title="MainWindow" Height="350" Width="525">  <Grid>    <StackPanel Orientation="Vertical">      <local:SingleCheckBox Content="我是一"/>      <local:SingleCheckBox Content="我是二"/>    </StackPanel>  </Grid></Window>

效果