Articles in categories

Now on Amazon

Reset a DependencyProperty to its default value

Say you have a DependencyProperty like this:

    public static readonly DependencyProperty ExpanderOrderDetailsHeaderProperty =
        DependencyProperty.Register("ExpanderOrderDetailsHeader", typeof(string), typeof(OrderHistoryViewModel), new UIPropertyMetadata("Order details."));
    public string ExpanderOrderDetailsHeader
      get { return (string)GetValue(ExpanderOrderDetailsHeaderProperty); }
      set { SetValue(ExpanderOrderDetailsHeaderProperty, value); }

The default value in this case is "Order details." During the program execution you change this value to something else. Now you want to reset the DependencyProperty to it's default value. 

It's pretty simple, just use the DependencyObject.ClearValue Method:


This will reset the DependencyProperty to it's default value, so in this case back to "Order details.".