WPF之自定义用户控件

C#代码:

     public partial class WPFUserControl : UserControl
    {
        public WPFUserControl()
        {
            InitializeComponent();
        }

        //定义依赖项属性
        public static DependencyProperty ColorProperty;
        public static DependencyProperty RedProperty;
        public static DependencyProperty GreenProperty;
        public static DependencyProperty BlueProperty;
        static WPFUserControl()
        {
            //注册依赖项属性
            ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(WPFUserControl), new PropertyMetadata(Colors.Black, new PropertyChangedCallback(OnColorChanged)));
            //DependencyProperty.Register(属性名称,属性类型,所属控件类型,注册依赖属性)
            //PropertyMetadata(默认值, PropertyChangedCallback更改时的回调函数)

            RedProperty = DependencyProperty.Register("Red", typeof(byte), typeof(WPFUserControl), new PropertyMetadata( new PropertyChangedCallback(OnColorRGBChanged)));
            BlueProperty = DependencyProperty.Register("Blue", typeof(byte), typeof(WPFUserControl), new PropertyMetadata( new PropertyChangedCallback(OnColorRGBChanged)));
            GreenProperty = DependencyProperty.Register("Green", typeof(byte), typeof(WPFUserControl), new PropertyMetadata( new PropertyChangedCallback(OnColorRGBChanged)));
        }
        //声明属性
        public Color Color
        {
            get { return (Color)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }
        public byte Red
        {
            get { return (byte)GetValue(RedProperty); }
            set { SetValue(RedProperty, value); }
        }
        public byte Blue
        {
            get { return (byte)GetValue(BlueProperty); }
            set { SetValue(BlueProperty, value); }
        }
        public byte Green
        {
            get { return (byte)GetValue(GreenProperty); }
            set { SetValue(GreenProperty, value); }
        }
        //声明回调函数方法
        private static void OnColorRGBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            WPFUserControl colorPicker = (WPFUserControl)sender;
            Color color = colorPicker.Color;
            if (e.Property == RedProperty)
            {
                color.R = (byte)e.NewValue;
            }
            else if (e.Property == GreenProperty)
            {
                color.G = (byte)e.NewValue;
            }
            else if (e.Property == BlueProperty)
            {
                color.B = (byte)e.NewValue;
            }
            colorPicker.Color = color;
        }
        //声明回调函数方法
        private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            WPFUserControl colorPicker = (WPFUserControl)sender;
            Color color = colorPicker.Color;
            Color oldColor = (Color)e.OldValue;
            Color newColor = (Color)e.NewValue;
            colorPicker.Red = newColor.R;
            colorPicker.Green = newColor.G;
            colorPicker.Blue = newColor.B;
            colorPicker.OnColorChanged(oldColor,newColor);
        }
        //注册路由事件
        private static readonly RoutedEvent ColorChangedEvent = EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<Color>), typeof(WPFUserControl));

        //RoutedEvent:表示和标识路由事件,并声明其特征。
        //EventManager类:提供事件相关的实用工具方法,这些方法可为类所有者注册路由事件,并添加类处理程序。
        //EventManager.RegisterRoutedEvent方法:向 Windows Presentation Foundation (WPF) 事件系统注册新的路由事件
        //EventManager.RegisterRoutedEvent(路由事件的名称,RoutingStrategy路由策略,事件处理程序的类型,路由事件的所有者类类型)
        //RoutedPropertyChangedEventHandler<T>:表示将处理跟踪属性值更改的各个路由事件的方法。

        //声明事件
        public event RoutedPropertyChangedEventHandler<Color> ColorChanged
        {
            add { AddHandler(ColorChangedEvent, value); }  
            //为指定的路由事件添加路由事件处理程序,并将该处理程序添加到当前元素的处理程序集合中。
            remove { RemoveHandler(ColorChangedEvent, value); } 
            //从此元素中删除指定的路由事件处理程序。
        }

        //事件引发
        private void OnColorChanged(Color oldValue, Color newValue)
        {
            RoutedPropertyChangedEventArgs<Color> args = new RoutedPropertyChangedEventArgs<Color>(oldValue,newValue);
            args.RoutedEvent = WPFUserControl.ColorChangedEvent;
            UIElement.RaiseEvent(args);//继承引发事件
            //RoutedPropertyChangedEventArgs<T> 类:提供有关依赖属性值更改的数据(由特定的路由事件报告),其中包含发生更改的属性的旧值和新值。
            //RaiseEvent:获取或设置与此 RoutedEventArgs 实例关联的 RoutedEvent。
            //RoutedEventArgs 类:包含与路由事件相关联的状态信息和事件数据。
            //RaiseEvent:引发特定路由事件。 在提供的 RoutedEventArgs 实例内标识将引发的 RoutedEvent(作为该事件数据的 RoutedEvent 属性)。
        }
    }

Xaml代码

<UserControl x:Class="wpf.WPFUserControl"
             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:wpf"
             Name="colorPicker">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <Slider Name="SliderRed" Minimum="0" Maximum="255" Margin="{Binding ElementName=colorPicker,Path=Padding}" Value="{Binding ElementName=colorPicker,Path=Red}"></Slider>
        <Slider Name="SliderGreen"  Grid.Row="1" Minimum="0" Maximum="255" Margin="{Binding ElementName=colorPicker,Path=Padding}" Value="{Binding ElementName=colorPicker,Path=Green}"></Slider>
        <Slider Name="SliderBlue"  Grid.Row="2" Minimum="0" Maximum="255" Margin="{Binding ElementName=colorPicker,Path=Padding}" Value="{Binding ElementName=colorPicker,Path=Blue}"></Slider>
        <Rectangle Grid.Column="1" Grid.RowSpan="3" Margin="{Binding ElementName=colorPicker,Path=Padding}" Width="50" Stroke="Black" StrokeThickness="1">
            <Rectangle.Fill>
                <SolidColorBrush Color="{Binding ElementName=colorPicker,Path=Color}"/>
            </Rectangle.Fill>

        </Rectangle> 
    </Grid>
</UserControl>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LADT-LINZI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值