Построение пользовательского элемента

WPF
  1. год назад

    Добрый день! Пытаюсь создать пользовательский элемент ColorPicker из темы https://professorweb.ru/my/WPF/Template/level18/18_2.php
    Но не получается генерировать событие ColorChanged.
    Вот мой код

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    
    namespace ColorPicker
    {
        /// <summary>
        /// Логика взаимодействия для ColorPicker.xaml
        /// </summary>
        public partial class ColorPicker : UserControl
        {
            static ColorPicker()
            {
                ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPicker), new FrameworkPropertyMetadata(Colors.Black, new PropertyChangedCallback(OncolorChanged)));
                RedProperty = DependencyProperty.Register("Red", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRgbChanged)));
                GreenProperty = DependencyProperty.Register("Green", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRgbChanged)));
                BlueProperty = DependencyProperty.Register("Blue", typeof(byte), typeof(ColorPicker), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnColorRgbChanged)));
    
                ColorChangedEvent = EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<Color>), typeof(ColorPicker));
            }
    
            public ColorPicker()
            {
                InitializeComponent();
            }
    
            #region Dependency properties
    
            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); }
            }
    
            #endregion
    
            #region Events
    
            public static readonly RoutedEvent ColorChangedEvent;
            
            public event RoutedPropertyChangedEventHandler<Color> ColorChanged
            {
                add { AddHandler(ColorChangedEvent, value); }
                remove { RemoveHandler(ColorChangedEvent, value); }
            }
    
            #endregion
    
            private static void OncolorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                ColorPicker colorpicker = (ColorPicker)sender;
                Color newColor = (Color)e.NewValue;
                colorpicker.Red = newColor.R;
                colorpicker.Green = newColor.G;
                colorpicker.Blue = newColor.B;
            }
    
            private static void OnColorRgbChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                ColorPicker colorPicker = (ColorPicker)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;
            }
        }
    }
    

    Подскажите в чем ошибка

или зарегистрируйтесь чтобы ответить