1 | initial version |
To establish a default value for a dependency property that is of a type derived from DependencyObject, follow these steps:
Here is an example of how to do this in C#:
public class MyClass : DependencyObject
{
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
"MyProperty", typeof(MyType), typeof(MyClass), new PropertyMetadata(new MyType()));
public MyType MyProperty
{
get { return (MyType)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
static MyClass()
{
MyPropertyProperty.OverrideMetadata(typeof(MyClass), new PropertyMetadata(new MyType()));
}
}
In this example, the MyProperty
dependency property is defined using the DependencyProperty.Register
method. In the static constructor of the class, the PropertyMetadata
is used to set the default value for the property. Finally, the DependencyObject.SetValue
method is used to set the value of the property to the default value.