WPF自定义Button按钮背景,支持鼠标滑动背景,不可用背景

上次在写button实现圆角的时候写了个支持按钮更换背景,支持滑动更换背景和不可用背景的样式。但是写的样式比较死,无法支持一套样式走天下。现在我跟该了下之前写的!

代码如下:

public class SunButton : Button
    {
        #region 属性
        /// 
        /// 圆角
        /// 
        public int BorderRadius
        {
            get { return (int)GetValue(BorderRadiusProperty); }
            set { SetValue(BorderRadiusProperty, value); }

        }

        public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register("BorderRadius", typeof(int), typeof(SunButton),new FrameworkPropertyMetadata());

        /// 
        /// 默认背景
        /// 
        public string DefaultBackground
        {
            get { return (string)GetValue(DefaultBackgroundProperty); }
            set { SetValue(DefaultBackgroundProperty, value); }
        }

        public static readonly DependencyProperty DefaultBackgroundProperty = DependencyProperty.Register("DefaultBackground", typeof(string), typeof(SunButton), new FrameworkPropertyMetadata());


        /// 
        /// 不可用背景
        /// 
        public string EnabledBackground
        {
            get { return (string)GetValue(EnabledBackgroundProperty); }
            set { SetValue(EnabledBackgroundProperty, value); }
        }

        public static readonly DependencyProperty EnabledBackgroundProperty = DependencyProperty.Register("EnabledBackground", typeof(string), typeof(SunButton), new FrameworkPropertyMetadata());

        /// 
        /// 鼠标移入
        /// 
        public string MouseOverBackground
        {
            get { return (string)GetValue(MouseOverBackgroundProperty); }
            set { SetValue(MouseOverBackgroundProperty, value); }
        }

        public static readonly DependencyProperty MouseOverBackgroundProperty = DependencyProperty.Register("MouseOverBackground", typeof(string), typeof(SunButton), new FrameworkPropertyMetadata());


        #endregion 属性

    }

这里定义了4个依赖属性,圆角、默认背景、鼠标移入、不可用背景。

下面样式:

红色部分就是怎么绑定依赖属性的值,由于定义的依赖属性是string类型的,所以需要用转换器吧图片地址换成图像类型,下面是转换器:
 public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                if (!string.IsNullOrEmpty(value.ToString()))
                {
                    return new BitmapImage(new Uri(value.ToString(), UriKind.RelativeOrAbsolute));
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

如何使用呢?

DefaultBackground="pack://application:,,,/Sun.WPF.Controls;component/Resources/Images/button/button_show.png"
                         MouseOverBackground="pack://application:,,,/Sun.WPF.Controls;component/Resources/Images/button/button_mouse.png"
                         EnabledBackground="pack://application:,,,/Sun.WPF.Controls;component/Resources/Images/button/button_enabled.png"
                         BorderRadius="5"
                         Style="{StaticResource SunButtonStyle}"
                         Foreground="White"
                         />

红色部分就是如何使用这些属性。

默认背景

移入背景

不可用背景



你可能感兴趣的:(wpf)