开发环境:Win10 + VS2017 + .Net4.5

这个 ColorPicker 是<<WPF编程宝典:使用C#2012和.NET4.5>>这本书中的例子.这里我记录一下,主要是为了加深印象,防止以后要开发自定义控件的时候忘记一些基本的步骤,可以随时来查一下.

调到博文的最后可以先查看一下效果图.

  1. 建立ColorPicker类继承自 Control



    为什么继承自 Control,Control类继承自UIElement -> FrameworkElement -> ColorPicker,同时 Control 类提供了 Template 功能.

  2. 通知WPF,将为控件提供新的样式.

    方法是在静态构造函数中调用 OverrideMetadata()方法.

    static ColorPicker()
    {
    DefaultStyleKeyProperty.OverrideMetadata(
    typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
    }
  3. 编写控件逻辑,添加一些必要的属性|事件|方法 ...

  4. 重写 OnApplyTemplate 方法,为模板中的元素添加数据绑定或者关联事件处理程序

  5. ColorPicker类的完整代码:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Windows.Media; namespace Demo.DIYControl.Controls.DeepInDIY

    {

    [TemplatePart(Name = RedSliderName,Type = typeof(RangeBase))]

    [TemplatePart(Name = GreenSliderName, Type = typeof(RangeBase))]

    [TemplatePart(Name = BlueSliderName, Type = typeof(RangeBase))]

    [TemplatePart(Name = PreviewBrushName, Type = typeof(SolidColorBrush))]

    public class ColorPicker:Control

    {

    private const string RedSliderName = "PART_RedSlider";

    private const string GreenSliderName = "PART_GreenSlider";

    private const string BlueSliderName = "PART_BlueSlider";

    private const string PreviewBrushName = "PART_PreviewBrush";
         private Brush _initializeBorderBrush;
    
         public const byte RGBMaxValue = 255;
    
         public byte Red
    {
    get { return (byte)GetValue(RedProperty); }
    set { SetValue(RedProperty, value); }
    }
    // Using a DependencyProperty as the backing store for Red. This enables animation, styling, binding, etc...
    public static readonly DependencyProperty RedProperty =
    DependencyProperty.Register(
    nameof(Red), typeof(byte), typeof(ColorPicker),
    new FrameworkPropertyMetadata(OnColorRGBChanged)); public byte Green
    {
    get { return (byte)GetValue(GreenProperty); }
    set { SetValue(GreenProperty, value); }
    }
    // Using a DependencyProperty as the backing store for Green. This enables animation, styling, binding, etc...
    public static readonly DependencyProperty GreenProperty =
    DependencyProperty.Register(
    nameof(Green), typeof(byte), typeof(ColorPicker),
    new FrameworkPropertyMetadata(OnColorRGBChanged)); public byte Blue
    {
    get { return (byte)GetValue(BlueProperty); }
    set { SetValue(BlueProperty, value); }
    }
    // Using a DependencyProperty as the backing store for Blue. This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BlueProperty =
    DependencyProperty.Register(
    nameof(Blue), typeof(byte), typeof(ColorPicker),
    new FrameworkPropertyMetadata(OnColorRGBChanged)); public Color Color
    {
    get { return (Color)GetValue(ColorProperty); }
    set { SetValue(ColorProperty, value); }
    }
    // Using a DependencyProperty as the backing store for Color. This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ColorProperty =
    DependencyProperty.Register(nameof(Color), typeof(Color),
    typeof(ColorPicker), new FrameworkPropertyMetadata(Colors.Black,OnColorChanged)); public CornerRadius CornerRadius
    {
    get { return (CornerRadius)GetValue(CornerRadiusProperty); }
    set { SetValue(CornerRadiusProperty, value); }
    }
    // Using a DependencyProperty as the backing store for CornerRadius. This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CornerRadiusProperty =
    DependencyProperty.Register(&quot;CornerRadius&quot;, typeof(CornerRadius), typeof(ColorPicker),
    new FrameworkPropertyMetadata(default(CornerRadius))); public bool UseDynamicBorder
    {
    get { return (bool)GetValue(UseDynamicBorderProperty); }
    set { SetValue(UseDynamicBorderProperty, value); }
    }
    // Using a DependencyProperty as the backing store for UseDynamicBorder. This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UseDynamicBorderProperty =
    DependencyProperty.Register(
    &quot;UseDynamicBorder&quot;, typeof(bool), typeof(ColorPicker),
    new FrameworkPropertyMetadata(true, OnUseDynamicBorderChanged)); public static readonly RoutedEvent ColorChangedEvent = EventManager.RegisterRoutedEvent(
    &quot;ColorChangedEvent&quot;, RoutingStrategy.Bubble,
    typeof(RoutedPropertyChangedEventHandler&lt;Color&gt;), typeof(ColorPicker));
    public event RoutedPropertyChangedEventHandler&lt;Color&gt; ColorChanged
    {
    add { AddHandler(ColorChangedEvent, value); }
    remove { RemoveHandler(ColorChangedEvent, value); }
    } static ColorPicker()
    {
    DefaultStyleKeyProperty.OverrideMetadata(
    typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
    } public ColorPicker()
    {
    Loaded += (sender, args) =&gt;
    {
    _initializeBorderBrush = BorderBrush; //save initial borderbrush
    BorderBrush = UseDynamicBorder? new SolidColorBrush(Color) : BorderBrush;
    };
    } private static void OnColorRGBChanged(DependencyObject sender,
    DependencyPropertyChangedEventArgs args)
    {
    ColorPicker colorPicker = sender as ColorPicker;
    colorPicker = colorPicker ?? throw new ArgumentException(); Color color = colorPicker.Color;
    if (args.Property == RedProperty)
    color.R = (byte)args.NewValue;
    else if (args.Property == GreenProperty)
    color.G = (byte)args.NewValue;
    else if (args.Property == BlueProperty)
    color.B = (byte)args.NewValue; colorPicker.Color = color;
    } private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    ColorPicker colorPicker = d as ColorPicker;
    d = d ?? throw new ArgumentException();
    colorPicker.Red = colorPicker.Color.R;
    colorPicker.Green = colorPicker.Color.G;
    colorPicker.Blue = colorPicker.Color.B; //set border color
    if (colorPicker.UseDynamicBorder)
    colorPicker.BorderBrush = new SolidColorBrush(colorPicker.Color); colorPicker.RaiseEvent(
    new RoutedEventArgs(ColorChangedEvent, e.NewValue));
    } private static void OnUseDynamicBorderChanged(
    DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
    ColorPicker colorPicker = d as ColorPicker;
    colorPicker = colorPicker ?? throw new ArgumentException(); colorPicker.BorderBrush = (bool)args.NewValue ? new SolidColorBrush(colorPicker.Color) : colorPicker._initializeBorderBrush;
    } public override void OnApplyTemplate()
    {
    base.OnApplyTemplate(); RangeBase slider = GetTemplateChild(&quot;PART_RedSlider&quot;) as RangeBase;
    if (slider != null)
    {
    Binding binding = new Binding()
    {
    Path = new PropertyPath(&quot;Red&quot;),
    Source = this,
    Mode = BindingMode.TwoWay
    };
    slider.SetBinding(RangeBase.ValueProperty, binding);
    slider.Maximum = RGBMaxValue;
    } slider = GetTemplateChild(&quot;PART_GreenSlider&quot;) as RangeBase;
    if (slider != null)
    {
    Binding binding = new Binding()
    {
    Path = new PropertyPath(nameof(Green)),
    Source = this,
    Mode = BindingMode.TwoWay
    };
    slider.SetBinding(RangeBase.ValueProperty, binding);
    slider.Maximum = RGBMaxValue;
    } slider = GetTemplateChild(&quot;PART_BlueSlider&quot;) as RangeBase;
    if (slider != null)
    {
    Binding binding = new Binding()
    {
    Path = new PropertyPath(nameof(Blue)),
    Source = this,
    Mode = BindingMode.TwoWay
    };
    slider.SetBinding(RangeBase.ValueProperty, binding);
    slider.Maximum = RGBMaxValue;
    } SolidColorBrush brush = GetTemplateChild(&quot;PART_PreviewBrush&quot;) as SolidColorBrush;
    if (brush != null)
    {
    Binding bd = new Binding
    {
    Path = new PropertyPath(nameof(brush.Color)),
    Source = brush,
    Mode = BindingMode.OneWayToSource
    };
    SetBinding(ColorPicker.ColorProperty, bd);
    }
    } }

    }

  6. 差不多就是这样一个简单的ColorPicker就出来了.另外增加一个UseDynamicBorder,来控制控件边框的颜色是否动态改变.

    看一下效果图

  </div>

WPF自定义控件开发实例 - ColorPicker的更多相关文章

  1. TemplateBinding与Binding区别,以及WPF自定义控件开发的遭遇

    在上一次的文章WPF OnApplyTemplate 不执行 或者执行滞后的疑惑谈到怎么正确的开发自定义控件,我们控件的样式中,属性的绑定一般都是用TemplateBinding来完成,如下一个基本的 ...

  2. WPF和Expression Blend开发实例:一个样式实现的数字输入框

    原文:WPF和Expression Blend开发实例:一个样式实现的数字输入框 今天来一个比较奇淫技巧的手法,很少人用,同时也不推荐太过频繁的使用. 先上样式: <Style x:Key=&q ...

  3. WPF开发实例——仿QQ登录界面

    原文:WPF开发实例--仿QQ登录界面 版权声明:本文为博主原创文章,如需转载请标明转载地址 http://blog.csdn.net/u013981858 https://blog.csdn.net ...

  4. WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...

  5. WPF自定义控件与样式(15)-终结篇 & 系列文章索引 & 源码共享

    系列文章目录  WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与样式(3)-TextBox & Ric ...

  6. WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要针对WPF项目 ...

  7. WPF自定义控件与样式(15)-终结篇

    原文:WPF自定义控件与样式(15)-终结篇 系列文章目录  WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与 ...

  8. WPF自定义控件创建

    WPF自定义控件创建 本文简单的介绍一下WPF自定义控件的开发. 首先,我们打开VisualStudio创建一个WPF自定义控件库,如下图: 然后,我们可以看到创建的解决方案如下: 在解决方案中,我们 ...

  9. 【转】WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要针对WPF项目 ...

随机推荐

  1. 【WebService】WebService之CXF和Spring整合(六)

    前面介绍了WebService与CXF的使用,项目中我们经常用到Spring,这里介绍CXF与Spring整合 步骤 1.创建一个Maven Web项目,可以参照:[Maven]Eclipse 使用M ...

  2. spring学习 十六 spring加载属性文件

    第一步:创建一个properties文件,以数据库链接作为实例db.properties jdbc.url=jdbc:mysql://192.168.153.128:3306/mybaties?cha ...

  3. ubuntu samba共享后windows读写文件都是以nogroup问题

    添加smb账号 sudo smbpasswd -a xxx 如果报错:Failed to add entry for user xxx 则是因为这个账号不存在 添加成功以后,过一会就可以重新登陆了(u ...

  4. 惊讶于word 的流畅

    word 这个产品 的操作流畅 比自家产品OneNote 比wps 强的太多 用后的体验是,再用其他的编译文字的软件,便感觉操作不畅,不流利,不舒服.(使人曾经沧海难为水,自然而然的不用别人的产品,w ...

  5. kbmmw 5.06.00 beta 发布

    原生.高效.可扩展.跨平台通信库来了. we are happy to announce v5.06.00 BETA of our popular middleware for Delphi and  ...

  6. Bootstrap之javascript组件

    一 模态框 模态框放在body标签里面的,是body标签的子元素 静态实例: <div class="modal fade" tabindex="-1" ...

  7. 2018.10.25 bzoj4517: [Sdoi2016]排列计数(组合数学)

    传送门 组合数学简单题. Ans=(nm)∗1Ans=\binom {n} {m}*1Ans=(mn​)∗1~(n−m)(n-m)(n−m)的错排数. 前面的直接线性筛逆元求. 后面的错排数递推式本蒟 ...

  8. 2.7 Sobel导数

    OpenCV函数 Sobel(src_gray,grad_x/grad_y,ddepth,x_order,y_order,scale,delta,BORDER_DEFAULT ) Scharr( ) ...

  9. PHP-CGI、FASTCGI和php-fpm的关系

    首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...

  10. netfilter框架和iptables

    转载自:http://blog.chinaunix.net/uid-23069658-id-3160506.html http://blog.chinaunix.net/uid-23069658-id ...