public class MyDependencyObject : DependencyObject { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(MyDependencyObject), new PropertyMetadata("Default Value")); public string MyProperty { get { return (string)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } }
public class ColorControl : Control { public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register("Foreground", typeof(Brush), typeof(ColorControl), new FrameworkPropertyMetadata(new SolidColorBrush(), FrameworkPropertyMetadataOptions.AffectsRender)); public Brush Foreground { get { return (Brush)GetValue(ForegroundProperty); } set { SetValue(ForegroundProperty, value); } } // Repeat the same code for Background and BorderColor properties. }In the above code block, we have created three DependencyProperty objects for our properties, and then used them to define the getter and setter for the three properties. We have also defined the default value for each property using the FrameworkPropertyMetadata constructor. The System.Windows.DependencyObject namespace is part of the WPF framework that is included in the .NET Framework package by Microsoft.