DependencyProperties和RoutedEvents的重叠功能

时间:2014-09-26 15:15:57

标签: c# wpf events mvvm dependency-properties

我还在进入WPF和MVVM。我写了一个ColorPicker控件,它基于我的原始控件ColorPickerBase,这是一个带有选择器拇指的colorWheel。

ColorPickerBase基本上看起来像这样(大幅削减以节省空间):

public class ColorPickerBase : Control
{
    #region Dependency Properties
    public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register("SelectedColor", typeof(Color), typeof(ColorPickerBase));

    public Color SelectedColor
    {
        get { return (Color)GetValue(SelectedColorProperty); }
        set { SetValue(SelectedColorProperty, value); UpdateThumbPosition(); }
    }
    #endregion // Dependency Properties

#region EventHandlers
    public delegate void ColorChangedEventHandler(object sender, Color newColor);
#endregion EventHandlers

    #region Routed Events
    public static readonly RoutedEvent SelectedColorChangedEvent = EventManager.RegisterRoutedEvent("SelectedColorChanged", RoutingStrategy.Bubble, typeof(ColorChangedEventHandler), typeof(ColorPickerBase));   

    public event ColorChangedEventHandler SelectedColorChanged
    {
        add { AddHandler(SelectedColorChangedEvent, value); }
        remove { RemoveHandler(SelectedColorChangedEvent, value); }
    }
    #endregion //Routed Events

    static ColorPickerBase()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPickerBase), new FrameworkPropertyMetadata(typeof(ColorPickerBase)));
    }

    #region Event Overrides
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        mColorThumb.DragDelta += OnColorThumbDragDelta;
    }
    #endregion //Event Overrides

    #region Events
    private void OnColorThumbDragDelta(object sender, DragDeltaEventArgs e)
    {
        //some calculations
        SelectedColor = Color.FromRgb(pixel[2], pixel[1], pixel[0]);
        // Trying to raise SelectedColorChanged here.
    }


    #endregion //Events
}

在我的ColorPicker课程中,我有一个ColorPickerBase的实例,并为SelectedColorChanged

添加处理程序
[TemplatePart(Name="PART_ColorSpectrum", Type = typeof(Controls.Primitives.ColorPickerBase))]
public class ColorPicker : Control
{
    #region Private Fields
    private Primitives.ColorPickerBase mColorSpectrum;
    #endregion //Private Fields

    #region Properties
    public Byte R
    {
        get { return mColorSpectrum.SelectedColor.R; }
        set { mColorSpectrum.SelectedColor = Color.FromArgb(mColorSpectrum.SelectedColor.A, value, mColorSpectrum.SelectedColor.G, mColorSpectrum.SelectedColor.B); }
    }
    public Byte G
    ...
    public Byte B
    ...
    #endregion //Properties

    #region Constructor
    static ColorPicker()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
    }

    public ColorPicker()
    {

    }
    #endregion //Constructor

    #region Event Overrides
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        mColorSpectrum = GetTemplateChild("PART_ColorSpectrum") as Primitives.ColorPickerBase;
        mColorSpectrum.SelectedColorChanged += OnSelectedColorChanged;
    }
    #endregion //Event Overrides

    #region Event Handlers
    void OnSelectedColorChanged(object sender, Color newColor)
    {
        R = newColor.R; G = newColor.G; B = newColor.B;
    }
    #endregion //Event Handlers
}

现在,根据我的理解,DependecyProperties已经以NotifyPropertyChanged的名义实现了这个确切的功能。我现在想知道如何在拥有的类ColorPicker中使用该功能。

0 个答案:

没有答案