1 .在类库里面添加system.xaml的引用,给控件指定Name;

2.设计控件的外观,并将内部元素绑定到控件类的属性;此时即使没有在类中增加相关属性也不会报错,xaml类似html错误只是不显示而已;

3.定义静态的依赖项;

4.定义依赖项的包装属性;

5.在静态构造函数中注册依赖项属性,注意设置回调函数;

6.实现回调函数

7 定义路由事件并注册

8.定义路由事件的包装器

9.触发路由事件

<UserControl x:Class="CustomControls.ColorPickerUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CustomControls"
             mc:Ignorable="d"
             Name="ColorPicker" Height="70" Width="285">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Slider Grid.Row="0" Grid.Column="0" Margin="{Binding ElementName=ColorPicker,Path=Padding}" 
                Minimum="0" Maximum="255" Value="{Binding ElementName=ColorPicker,Path=Red}"
                VerticalAlignment="Center"></Slider>
        <Slider Grid.Row="1" Grid.Column="0" Margin="{Binding ElementName=ColorPicker,Path=Padding}" 
                Minimum="0" Maximum="255" Value="{Binding ElementName=ColorPicker,Path=Green}"
                VerticalAlignment="Center"></Slider>
        <Slider Grid.Row="2" Grid.Column="0" Margin="{Binding ElementName=ColorPicker,Path=Padding}" 
                Minimum="0" Maximum="255" Value="{Binding ElementName=ColorPicker,Path=Blue}" 
                VerticalAlignment="Center"></Slider>
        <Rectangle Grid.Row="0" Grid.Column="1" Grid.RowSpan="3" Width="100" Margin="{Binding ElementName=ColorPicker,Path=Padding}"
                   Stroke="Black" StrokeThickness="1">
            <Rectangle.Fill>
                <SolidColorBrush Color="{Binding ElementName=ColorPicker,Path=Color}"></SolidColorBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>

</UserControl>

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 CustomControls
{
    /// <summary>
    /// ColorPickerUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class ColorPickerUserControl : UserControl
    {
        public ColorPickerUserControl()
        {
            InitializeComponent();
        }

        public static DependencyProperty ColorProperty;
        public Color Color
        {
            get
            {
                return (Color)GetValue(ColorProperty);
            }
            set
            {
                SetValue(ColorProperty, value);
            }
        }

        public static DependencyProperty RedProperty;
        public byte Red
        {
            get
            {
                return (byte)GetValue(RedProperty);
            }
            set
            {
                SetValue(RedProperty, value);
            }
        }

        public static DependencyProperty GreenProperty;
        public byte Green
        {
            get
            {
                return (byte)GetValue(GreenProperty);
            }
            set
            {
                SetValue(GreenProperty, value);
            }
        }

        public static DependencyProperty BlueProperty;
        public byte Blue
        {
            get
            {
                return (byte)GetValue(BlueProperty);
            }
            set
            {
                SetValue(BlueProperty, value);
            }
        }

        static ColorPickerUserControl()
        {
            ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPickerUserControl), 
                new PropertyMetadata(Colors.Black, new PropertyChangedCallback(OnColorChanged)));
            RedProperty = DependencyProperty.Register("Red", typeof(byte), typeof(ColorPickerUserControl),
                new PropertyMetadata((byte)0, new PropertyChangedCallback(OnRGBChanged)));
            GreenProperty = DependencyProperty.Register("Green", typeof(byte), typeof(ColorPickerUserControl),
                new PropertyMetadata((byte)0, new PropertyChangedCallback(OnRGBChanged)));
            BlueProperty = DependencyProperty.Register("Blue", typeof(byte), typeof(ColorPickerUserControl),
                new PropertyMetadata((byte)0, new PropertyChangedCallback(OnRGBChanged)));
        }

        private static void OnRGBChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPickerUserControl colorPicker = (ColorPickerUserControl)d;
            Color color = colorPicker.Color;
            if (e.Property == RedProperty)
            {
                color.R = (byte)e.NewValue;
            }
            else if (e.Property == GreenProperty)
            {
                color.G = (byte)e.NewValue;
            }
            else
            {
                color.B= (byte)e.NewValue;
            }
            colorPicker.Color = color;

        }

        private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPickerUserControl colorPicker = (ColorPickerUserControl)d;
            Color newColor = (Color)e.NewValue;
            colorPicker.Red = newColor.R;
            colorPicker.Green = newColor.G;
            colorPicker.Blue = newColor.B;
            Color oldColor = (Color)e.OldValue;
            RoutedPropertyChangedEventArgs<Color> args = new RoutedPropertyChangedEventArgs<Color>(oldColor,newColor);
            args.RoutedEvent = ColorChangedEvent;
            colorPicker.RaiseEvent(args);

}

public static readonly RoutedEvent ColorChangedEvent = EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble, 
            typeof(RoutedPropertyChangedEventHandler<Color>), typeof(ColorPickerUserControl));
        public event RoutedPropertyChangedEventHandler<Color> ColorChanged
        {
            add
            {
                AddHandler(ColorChangedEvent, value);
            }
            remove
            {
                RemoveHandler(ColorChangedEvent, value);
            }

        }

    }

}

<Window x:Class="CustomControlDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CustomControlDemo"
        xmlns:lib ="clr-namespace:CustomControls;assembly=CustomControls"
        mc:Ignorable="d"
        Title="MainWindow" Height="309.6" Width="348.2">
    <Grid>
        <StackPanel>
            <TextBlock Name="tbColor" Margin="3">111</TextBlock>//放在usercontrol前面否则设置color时候会报错

            <lib:ColorPickerUserControl Name="colorPicker" Height="Auto" Width="Auto" Margin="3" Padding="3" Color="Red"
                                    ColorChanged="ColorPickerUserControl_ColorChanged"></lib:ColorPickerUserControl>
        </StackPanel>
        
    </Grid>

</Window>

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 CustomControlDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ColorPickerUserControl_ColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            Color color = (Color)e.NewValue;
            tbColor.Text = "The new Color is" + color.ToString();
        }
    }
}

WPF自定义控件步骤的更多相关文章

  1. WPF 如何创建自己的WPF自定义控件库

    在我们平时的项目中,我们经常需要一套自己的自定义控件库,这个特别是在Prism这种框架下面进行开发的时候,每个人都使用一套统一的控件,这样才不会每个人由于界面不统一而造成的整个软件系统千差万别,所以我 ...

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

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

  3. WPF自定义控件与样式(1)-矢量字体图标(iconfont)

    一.图标字体 图标字体在网页开发上运用非常广泛,具体可以网络搜索了解,网页上的运用有很多例子,如Bootstrap.但在C/S程序中使用还不多,字体图标其实就是把矢量图形打包到字体文件里,就像使用一般 ...

  4. WPF自定义控件与样式(2)-自定义按钮FButton

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

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

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

  6. WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式

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

  7. WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

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

  8. WPF自定义控件与样式(6)-ScrollViewer与ListBox自定义样式

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

  9. WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式

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

随机推荐

  1. Java 常用工具类---- 各种字符集编码判断与转换

    import java.io.UnsupportedEncodingException; /** * 判断字符编码 * * @author guyinyihun */ public class Cha ...

  2. 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(2)准备知识

    一.变量为什么必须初始化? 在回答这个问题之前,我们先来运行一段代码: #include <stdio.h> int main(){ int i; printf("i=%d\n& ...

  3. Ajax详解及使用Ajax时的返回值类型有哪些?

    Ajax详解 Ajax = 异步 JavaScript 和 XML. Ajax 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新.这意味着可以在 ...

  4. active set method(激活集方法)

    在优化问题的求解中,如果待优化(最大最小)的目标函数,其解集受限于一组约束条件, g1(x)≥0,-,gk(x)≥0 约束条件定义着可行域(feasible region),对于可行域中的任一点 x ...

  5. 数组[0]和[firstobject]的区别

    数组[0]和[firstobject]的区别   [0]:数组为空时回报错 [firstobject]:数组为空时回返回nil

  6. KVM,QEMU核心分析

    现在的问题是学习虚拟化软件KVM相关实施原则.处理,的源代码的分析总结,,若有不对的地方,希望大家提出. 因为有一些代码结构图或者是架构图上传比較麻烦.所以博文都放在了自己的个人博客上.麻烦大家移步查 ...

  7. 图片拉伸:IOS开发UIImage中stretchableImageWithLeftCapWidth

    意思就是用来创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的宽度,第二个参数是上面不拉伸的高度.那么接下来的一个像素会被拉伸.例如,leftCapHeight为6,top ...

  8. Erlang/OTP 中文手册

    http://erldoc.com/ Open Telecom Platform application array asn1rt base64 binary calendar code dbg di ...

  9. Web前端实践经验总结

    最近用了不少业余时间,在加强Web前端.有个很大的感触就是,web前端比较麻烦,主要是布局和样式.最主要的原因,还是之前实践得比较少,熟能生巧,不得不服啊. 自己从头开始写布局和css太费心思了,比较 ...

  10. 【9112】求2的n次方的精确值

    Time Limit: 1 second Memory Limit: 2 MB 问题描述 求2^n的精确值.n由用户输入,0<=n<=3232. Input 输入只有一行,一个正整数n. ...