#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
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: