#119 – Read and Write Dependency Property Values from XAML or Code
November 8, 2010 3 Comments
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.
Pingback: Tweets that mention #119 – Read and Write Dependency Property Values from XAML or Code « 2,000 Things You Should Know About WPF -- Topsy.com
They don’t actually need to be dependency properties to do this, regular properties will also work provided they have a public setter.
You’re correct. This post is talking about how dependency properties behave when set from XAML. I have another post where I talk about setting CLR properties and what the differences are.