#120 – Clearing a Dependency Property Value

You can set the value of a dependency property from either code or XAML and that local value will override any inherited or default values.

You can also clear a property value using the DependencyObject.ClearValue method.  This will undo any setting of a local property value, so that the value reverts to the inherited or default value.  (The property value can also come from one of several other sources).

            Person p = new Person("Samuel", "Clemens");
            Console.WriteLine(p.Age);  // 18  (the default)

            // Setting a property value
            p.Age = 70;
            Console.WriteLine(p.Age);  // 70

            // Clear a property value
            p.ClearValue(Person.AgeProperty);
            Console.WriteLine(p.Age);  // 18 again
Advertisements