#119 – Read and Write Dependency Property Values from XAML or Code

You can read and write dependency property values in either XAML or code.

For example, if we implement dependency properties FirstName, LastName and Age in our Person class, we can set these properties from XAML:

        <m:Person x:Key="guy" FirstName="Samuel" LastName="Clemens"  Age="75"/>

We can also read or write these properties from our code:

            Person author = (Person)this.Resources["guy"];
            string info = string.Format("{0} {1}, aged {2}", author.FirstName, author.LastName, author.Age);

Because these properties are full-fledged WPF dependency properties, they support the normal range of dependency property functionality including data binding, property inheritance and change notification.