WPF开源控件Newbeecoder.UI 复选框样式

在上一章讲了单选框样式,单选扩展依赖属性类继承于RadioButton。然后在自定义样式,RadioButton中需要把GroupName设置为同一组名称即可。

复选框实现原理同单选框相同,主要区别在于实现多选需要继承ToggleButton类,此类每个按钮是可以单独选择的。

先看一下实现效果:

WPF开源控件Newbeecoder.UI 复选框样式

增加几项依赖属性和单选框一样。这里就不再多介绍。

/// <summary>
    /// 多选框
    /// </summary>
    public class NbCheckBox : NbToggleButton
    {
        #region ctor

        static NbCheckBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NbCheckBox), new FrameworkPropertyMetadata(typeof(NbCheckBox)));
        }

        /// <summary>
        ///
        /// </summary>
        public NbCheckBox()
        {
            IsHitTestVisibleInChrome = true;
        }

        #endregion ctor

        public Brush OptionMarkBackground
        {
            get { return (Brush)GetValue(OptionMarkBackgroundProperty); }
            set { SetValue(OptionMarkBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for OptionMarkBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OptionMarkBackgroundProperty =
            DependencyProperty.Register("OptionMarkBackground", typeof(Brush), typeof(NbCheckBox), new PropertyMetadata(Brushes.Transparent));

        public bool ShowOptionMark
        {
            get { return (bool)GetValue(ShowOptionMarkProperty); }
            set { SetValue(ShowOptionMarkProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowOptionMark.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowOptionMarkProperty =
            DependencyProperty.Register("ShowOptionMark", typeof(bool), typeof(NbCheckBox), new PropertyMetadata(true));

        public double OptionMarkHeight
        {
            get { return (double)GetValue(OptionMarkHeightProperty); }
            set { SetValue(OptionMarkHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowOptionMarkHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OptionMarkHeightProperty =
            DependencyProperty.Register("OptionMarkHeight", typeof(double), typeof(NbCheckBox), new PropertyMetadata(16d));

        public double OptionMarkWidth
        {
            get { return (double)GetValue(OptionMarkWidthProperty); }
            set { SetValue(OptionMarkWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ShowOptionMarkWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OptionMarkWidthProperty =
            DependencyProperty.Register("OptionMarkWidth", typeof(double), typeof(NbCheckBox), new PropertyMetadata(16d));
    }

复选框样式代码如下:

WPF开源控件Newbeecoder.UI 复选框样式

复选功能就实现了,接下来就是调用代码示例:

WPF开源控件Newbeecoder.UI 复选框样式

上一篇:关于Xml序列化中使用抽象类导致无法正确序列化而且修改方法过于讨厌这件事


下一篇:附加属性的使用