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

Advertisements

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

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

  1. 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

  2. MarkF says:

    They don’t actually need to be dependency properties to do this, regular properties will also work provided they have a public setter.

    • Sean says:

      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.

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: