Thursday, July 30, 2009

Creating DependencyProperties for a Class

The following code is needed if you want to animate Padding.Top of a control.

public class MyClass : UserControl
{
public static readonly DependencyProperty PaddingTopProperty = DependencyProperty.Register("PaddingTop", typeof(double), typeof(MyClass), new PropertyMetadata(0d, new PropertyChangedCallback(OnPaddingTopPropertyChanged)));

public double PaddingTop
{
get
{
return Padding.Top;
}
set
{
Padding = new Thickness(Padding.Left, value, Padding.Right, Padding.Bottom);
}
}

private static void OnPaddingTopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as MyClass).PaddingTop = (double)e.NewValue;
}
}

No comments:

Post a Comment