WP7备注(27)(DependencyProperty|RoutedPropertyChangedEventHandler)

public static readonly DependencyProperty Color1Property =
DependencyProperty.Register("Color1",
typeof(Color),
typeof(CustomButtonButton),
new PropertyMetadata(Colors.Black, OnColorChanged));
public static readonly DependencyProperty Color2Property =
DependencyProperty.Register("Color2",
typeof(Color),
typeof(CustomButton),
new PropertyMetadata(Colors.White, OnColorChanged));
public Color Color1
{
set { SetValue(Color1Property, value); }
get { return (Color)GetValue(Color1Property); }
}
public Color Color2
{
set { SetValue(Color2Property, value); }
get { return (Color)GetValue(Color2Property); }
}
static void OnColorChanged(DependencyObject obj,
DependencyPropertyChangedEventArgs args)
{
CustomButton btn = obj as CustomButton;
if (args.Property == Color1Property)
btn.gradientStop1.Color = (Color)args.NewValue;
if (args.Property == Color2Property)
btn.gradientStop2.Color = (Color)args.NewValue;
}

DependencyPropertyChangedEventArgs 的参数里存在着Property属性指向改变的DependencyProperty,并存在着NewValue属性指向改变后的新值,OldValue属性指向改变前的旧值

同时,存在一种简版的方法:

static void OnColorChanged(DependencyObject obj,
DependencyPropertyChangedEventArgs args)
{
CustomButton btn = obj as CustomButton;
btn.gradientStop1.Color = btn.Color1;
btn.gradientStop2.Color = btn.Color2;
}

申明为DependencyProperty的属性,可以进行Style设定:

<phone:PhoneApplicationPage.Resources>
<Style x:Key="customButtonStyle"
TargetType="local:CustomButton">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Color1" Value="Cyan" />
<Setter Property="Color2" Value="Pink" />
</Style>
</phone:PhoneApplicationPage.Resources>

------------------------------------------------------------------------------------------

RoutedPropertyChangedEventHandler可以提供一个共外部使用的事件处理:

public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value",
typeof(byte),
typeof(ColorColumn),
new PropertyMetadata((byte)0, OnValueChanged));
…
public event RoutedPropertyChangedEventHandler<byte> ValueChanged;
…
public byte Value
{
set { SetValue(ValueProperty, value); }
get { return (byte)GetValue(ValueProperty); }
}
…
static void OnValueChanged(DependencyObject obj,
DependencyPropertyChangedEventArgs args)
{
(obj as ColorColumn).OnValueChanged((byte)args.OldValue,
(byte)args.NewValue);
}
protected virtual void OnValueChanged(byte oldValue, byte newValue)
{
slider.Value = newValue;
colorValue.Text = newValue.ToString("X2");
if (ValueChanged != null)
ValueChanged(this,
new RoutedPropertyChangedEventArgs<byte>(oldValue, newValue));
}
…

转载于:https://www.cnblogs.com/otomii/archive/2011/04/30/2033331.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值