Read-only Dependency Properties

In the previous article, titled Dependency Properties, we explored how to create dependency properties for our class. In this article, we are going to explore how to create dependency properties that can only be updated in the defining class itself. As a result, these properties are read-only.

The example for this article can be found in the https://github.com/atzimler/WpfWednesday repository under the Z07_ReadOnlyDependencyProperties directory.

The AddCalculation class has three properties, A, B and Result. A and B properties are read-write properties and changing any of them will update the Result property with the sum of the two numbers. To be able to implement DependencyProperties, the class needs to be derived directly or indirectly from the DependencyObject class.

How read-only dependency properties work

Read-only dependency properties are like the normal dependency properties, except that during the registration instead of a DependencyProperty object, a DependencyPropertyKey object is given back. To update the value, you need the DependencyPropertyKey, while to read it you need a DependencyProperty object.

To register the property, we declare a private static member of the class, so that no code outside of the class can access the value from it.

Then, we publish the DependencyProperty part of the DependencyPropertyKey, so that if there is a code trying to bind to the read-only property it will be possible.

Finally, we implement a read-only accessor property, so that code that does not need binding, can access the properties through a convenient API call.

Here are the unit tests from the codebase in case you are only interested in reading how it works:

And one more thing… I promised in the previous article that we are going to review how to attach event handlers to DependencyProperties during their definitions. The second parameter of the PropertyMetadata class’s constructor allows you to do this. This is used for both A and B properties to update the Result property at the same time.

You can connect with me on LinkedIn, Twitter or you can register on the blog. Share if you found this article useful or know somebody who would benefit from reading it.

By subscribing to the email list, you will be notified about new posts.
Loading

3 Responses to Read-only Dependency Properties

  1. Attila Tamas Zimler April 29, 2017 at 9:51 am #

    Posted on Saturday, because of technical difficulties 🙂 – Sorry, it is not Wednesday 😀

  2. Claudio Liesch November 20, 2020 at 7:09 pm #

    Virtually all of whatever you say happens to be supprisingly accurate and that makes me wonder why I hadn’t looked at this with this light before. Your piece truly did turn the light on for me as far as this particular subject matter goes. Nevertheless there is one particular point I am not too comfy with and whilst I try to reconcile that with the main theme of your point, let me observe just what all the rest of the visitors have to say.Nicely done.

    • Attila Tamas Zimler November 25, 2020 at 11:53 am #

      Hi Claudio, Thank you for your feedback. Would you be interested in discussing the point that you are trying to reconcile?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress. Designed by WooThemes