WPF 自定义 CheckBox

前面提到的CheckBox360是并不完善的,今天对他进行完善一下,去掉了Convert 的使用。主要还是包装成DLL来使用的


一、控件界面设计  

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Sys="clr-namespace:System;assembly=mscorlib"
                    >
    
    <!--CheckBox样式 开 描述-->
    <Sys:String x:Key="CheckBoxOnText">开</Sys:String>
    <!--CheckBox样式 关 描述-->
    <Sys:String x:Key="CheckBoxOFFText">关</Sys:String>
    <Color x:Key="BtnBack">White</Color>
    <Color x:Key="BtnCenterBorderBack">Gray</Color>
    <!--CheckBox样式-->
    <Style TargetType="CheckBox" x:Key="CheckBoxStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CheckBox">
                    <Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" CornerRadius="7" Background="{TemplateBinding Background}" BorderThickness="0">
                        <Grid>
                            <Grid Name="Btn" Width="{TemplateBinding Height}" Height="{TemplateBinding Height}" >
                                <Ellipse Margin="2" Panel.ZIndex="1">
                                    <Ellipse.Fill>
                                        <RadialGradientBrush RadiusX="1" RadiusY="1" GradientOrigin="0.5,0.5">
                                            <GradientStop Color="{DynamicResource BtnBack}" Offset="0.3"></GradientStop>
                                            <GradientStop Color="{DynamicResource BtnCenterBorderBack}" Offset="0.32"></GradientStop>
                                            <GradientStop Color="{DynamicResource BtnBack}" Offset="0.34"></GradientStop>
                                        </RadialGradientBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </Grid>
                            <TextBlock Name="Flag" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,0,5,0" Panel.ZIndex="0"  FontWeight="Bold"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="Btn" Property="HorizontalAlignment" Value="Right"></Setter>
                            <Setter TargetName="Flag" Property="HorizontalAlignment" Value="Left"></Setter>
                            <Setter TargetName="Flag" Property="Text" Value="{DynamicResource CheckBoxOnText}"></Setter>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter TargetName="Btn" Property="HorizontalAlignment" Value="Left"></Setter>
                            <Setter TargetName="Flag" Property="HorizontalAlignment" Value="Right"></Setter>
                            <Setter TargetName="Flag" Property="Text" Value="{DynamicResource CheckBoxOFFText}"></Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

二、后台界面。添加了几个依赖属性,便于前台使用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace DialogEx.Controls
{
    public class CheckBox360 : CheckBox
    {
        public static readonly DependencyProperty ContentOnProperty = DependencyProperty.Register("ContentOn", typeof(string), typeof(CheckBox360), new PropertyMetadata(string.Empty));
        public static readonly DependencyProperty ContentOffProperty = DependencyProperty.Register("ContentOff", typeof(string), typeof(CheckBox360), new PropertyMetadata(string.Empty));
        public static readonly DependencyProperty ButtonBackgroundProperty = DependencyProperty.Register("ButtonBackground", typeof(Brush), typeof(CheckBox360), new PropertyMetadata(Brushes.White));
        public static readonly DependencyProperty ButtonCenterBorderProperty = DependencyProperty.Register("ButtonCenterBorder", typeof(Brush), typeof(CheckBox360), new PropertyMetadata(Brushes.Gray));

        /// <summary>
        /// On 内容
        /// </summary>
        public string ContentOn
        {
            get { return (string)GetValue(ContentOnProperty); }
            set { SetValue(ContentOnProperty, value); }
        }

        /// <summary>
        /// off 内容
        /// </summary>
        public string ContentOff
        {
            get { return (string)GetValue(ContentOffProperty); }
            set { SetValue(ContentOffProperty, value); }
        }

        /// <summary>
        /// 圆饼按钮颜色
        /// </summary>
        public Brush ButtonBackground
        {
            get { return (Brush)GetValue(ButtonBackgroundProperty); }
            set { SetValue(ButtonBackgroundProperty, value); }
        }

        /// <summary>
        /// 圆饼按钮中间Border颜色
        /// </summary>
        public Brush ButtonCenterBorder
        {
            get { return (Brush)GetValue(ButtonCenterBorderProperty); }
            set { SetValue(ButtonCenterBorderProperty, value); }
        }
        
        public CheckBox360()
        {
            Loaded += (sender, args) =>
            {
                try
                {
                    this.Resources.Source = new Uri("DialogEx;Component/Controls/CheckBox360.xaml", UriKind.RelativeOrAbsolute);
                }
                catch
                {
                    throw new Exception("未找到:DialogEx;Component/Controls/CheckBox360.xaml");
                }
                this.Resources["BtnBack"] = (Color)ColorConverter.ConvertFromString(ButtonBackground.ToString());
                this.Resources["BtnCenterBorderBack"] = (Color)ColorConverter.ConvertFromString(ButtonCenterBorder.ToString()); 
                this.Resources["CheckBoxOnText"] = ContentOn;
                this.Resources["CheckBoxOFFText"] = ContentOff;
                this.Style = this.Resources["CheckBoxStyle"] as Style;
            };
        }
    }
}



三、使用方法 

xmlns:MyControls="clr-namespace:DialogEx.Controls;assembly=DialogEx"

<MyControls:CheckBox360 Margin="5" Width="65" Height="23" Foreground="White" Background="#6CC614" ButtonBackground="White" ButtonCenterBorder="Gray" ContentOn="启用" ContentOff="禁用"></MyControls:CheckBox360>



关于依赖属性实时跟新问题请见 

http://blog.csdn.net/lvguoshan/article/details/47835957



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值