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), typeof(WPFUserControl));
//RoutedEvent:表示和标识路由事件,并声明其特征。
//EventManager类:提供事件相关的实用工具方法,这些方法可为类所有者注册路由事件,并添加类处理程序。
//EventManager.RegisterRoutedEvent方法:向 Windows Presentation Foundation (WPF) 事件系统注册新的路由事件
//EventManager.RegisterRoutedEvent(路由事件的名称,RoutingStrategy路由策略,事件处理程序的类型,路由事件的所有者类类型)
//RoutedPropertyChangedEventHandler:表示将处理跟踪属性值更改的各个路由事件的方法。
//声明事件
public event RoutedPropertyChangedEventHandler ColorChanged
{
add { AddHandler(ColorChangedEvent, value); }
//为指定的路由事件添加路由事件处理程序,并将该处理程序添加到当前元素的处理程序集合中。
remove { RemoveHandler(ColorChangedEvent, value); }
//从此元素中删除指定的路由事件处理程序。
}
//事件引发
private void OnColorChanged(Color oldValue, Color newValue)
{
RoutedPropertyChangedEventArgs args = new RoutedPropertyChangedEventArgs(oldValue,newValue);
args.RoutedEvent = WPFUserControl.ColorChangedEvent;
UIElement.RaiseEvent(args);//继承引发事件
//RoutedPropertyChangedEventArgs 类:提供有关依赖属性值更改的数据(由特定的路由事件报告),其中包含发生更改的属性的旧值和新值。
//RaiseEvent:获取或设置与此 RoutedEventArgs 实例关联的 RoutedEvent。
//RoutedEventArgs 类:包含与路由事件相关联的状态信息和事件数据。
//RaiseEvent:引发特定路由事件。 在提供的 RoutedEventArgs 实例内标识将引发的 RoutedEvent(作为该事件数据的 RoutedEvent 属性)。
}
}
Xaml代码
"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>
"auto"/>
"auto"/>
"auto"/>
Grid.RowDefinitions>
<Grid.ColumnDefinitions>
"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>
"{Binding ElementName=colorPicker,Path=Color}"/>
Rectangle.Fill>
Rectangle>
Grid>