WPF 依赖属性 回调函数

快捷键:propdp + Tab + Tab

public int MyProperty
{
    get { return (int)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

PropertyMetadata

/// <summary> Initializes a new instance of the <see cref="T:System.Windows.PropertyMetadata" /> class. </summary>
public PropertyMetadata()
{
}

/// <summary> Initializes a new instance of the <see cref="T:System.Windows.PropertyMetadata" /> class with a specified default value for the dependency property that this metadata will be applied to. </summary>
/// <param name="defaultValue">The default value to specify for a dependency property, usually provided as a value of some specific type.</param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="defaultValue" /> cannot be set to the value <see cref="F:System.Windows.DependencyProperty.UnsetValue" />; see Remarks.</exception>
public PropertyMetadata(object defaultValue)
{
  this.DefaultValue = defaultValue;
}

/// <summary> Initializes a new instance of the <see cref="T:System.Windows.PropertyMetadata" /> class with the specified <see cref="T:System.Windows.PropertyChangedCallback" /> implementation reference. </summary>
/// <param name="propertyChangedCallback">Reference to a handler implementation that is to be called by the property system whenever the effective value of the property changes.</param>
public PropertyMetadata(PropertyChangedCallback propertyChangedCallback)
{
  this.PropertyChangedCallback = propertyChangedCallback;
}

/// <summary> Initializes a new instance of the <see cref="T:System.Windows.PropertyMetadata" /> class with the specified default value and <see cref="T:System.Windows.PropertyChangedCallback" /> implementation reference. </summary>
/// <param name="defaultValue">The default value of the dependency property, usually provided as a value of some specific type.</param>
/// <param name="propertyChangedCallback">Reference to a handler implementation that is to be called by the property system whenever the effective value of the property changes.</param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="defaultValue" /> cannot be set to the value <see cref="F:System.Windows.DependencyProperty.UnsetValue" />; see Remarks.</exception>
public PropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback)
{
  this.DefaultValue = defaultValue;
  this.PropertyChangedCallback = propertyChangedCallback;
}

/// <summary>Initializes a new instance of the <see cref="T:System.Windows.PropertyMetadata" /> class with the specified default value and callbacks. </summary>
/// <param name="defaultValue">The default value of the dependency property, usually provided as a value of some specific type.</param>
/// <param name="propertyChangedCallback">Reference to a handler implementation that is to be called by the property system whenever the effective value of the property changes.</param>
/// <param name="coerceValueCallback">Reference to a handler implementation that is to be called whenever the property system calls <see cref="M:System.Windows.DependencyObject.CoerceValue(System.Windows.DependencyProperty)" /> against this property.</param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="defaultValue" /> cannot be set to the value <see cref="F:System.Windows.DependencyProperty.UnsetValue" />; see Remarks.</exception>
public PropertyMetadata(
  object defaultValue,
  PropertyChangedCallback propertyChangedCallback,
  CoerceValueCallback coerceValueCallback)
{
  this.DefaultValue = defaultValue;
  this.PropertyChangedCallback = propertyChangedCallback;
  this.CoerceValueCallback = coerceValueCallback;
}

PropertyChangedCallback

/// <summary>Represents the callback that is invoked when the effective property value of a dependency property changes.</summary>
/// <param name="d">The <see cref="T:System.Windows.DependencyObject" /> on which the property has changed value.</param>
/// <param name="e">Event data that is issued by any event that tracks changes to the effective value of this property.</param>
public delegate void PropertyChangedCallback(
DependencyObject d,
DependencyPropertyChangedEventArgs e);

Demo

public class DependencyPropertyDemo : DependencyObject
{
    // 快捷键:propdp + Tab + Tab
    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }
    
    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(int), typeof(DependencyPropertyDemo), new PropertyMetadata(0, MyPropertyChanged));
        
    private static void MyPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        MessageBox.Show(String.Format("MyPropertyChanged 属性:{0} 新值:{1} 旧值:{2}", e.Property.Name, e.NewValue, e.OldValue));
    }
}

知识共享许可协议

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。

转载于:https://www.cnblogs.com/MingsonZheng/p/11087758.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值