快捷键: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) 。