CodeMonkey
CodeMonkey

Reputation: 129

How do DependencyProperties work?

I have a question about WPF dependencyproperty. When the dependencyproperty is registered, it is registered as public static readonly.

  1. If it is readonly, how can the value be changed?
  2. If it is static, then there is only one instance. Let's say I have a dependencyproperty someproperty for class button. If I have two buttons that have different values of someproperty. How does WPF handle it?

I am a little confused. Thank you in advance for your help.

Upvotes: 1

Views: 260

Answers (4)

kubal5003
kubal5003

Reputation: 7254

DependencyProperties work similar to a Dictionary<object,object>. You always set the value for an object, and not a value for the DependencyProperty itself. That's why it can be static. It's also static because it's faster this way.

Upvotes: 0

Samuel Slade
Samuel Slade

Reputation: 8613

When you change the value of the DependencyProperty, you are doing just that: changing it's value. You are not changing the reference to the DependencyProperty instance. Therefore, it is valid to have it marked as readonly. It is similar to having an instance of some class. You can mark that instance readonly while being still able to modify the internal values of the class. i.e.:

private readonly List<string> mSomeCollection = new List<string>();

// Modifying the collection is allowed
mSomeCollection.Add(...);
mSomeCollection.Remove(...);

// Re-assigning the mSomeCollection variable is disallowed with readonly specified
mSomeCollection = new List<string>();

As for the static instance question, I believe it has something to do with the way DependencyProperty values are set - they are set according to the target of the property, as opposed to the property itself (I think). I'm not entirely sure how this part works (I need to read up on it), but take a look over on MSDN for an overview on dependency properties, which may hold a clue to this.

Upvotes: 1

Csaba Fabian
Csaba Fabian

Reputation: 876

The object you receive back from Register(...) is the descriptor of the DependencyProperty. That one is read-only; you cannot modify the properties of the property - the metadata of the property, if you like. (For example, you can't modify the inheritance behaviour.)

You can, however, modify the value of the property, using the SetValue(...) method. And in order to use your property from XAML, you should declare a wrapper CLR property for your DependencyProperty, as in the example in the linked article.

Upvotes: 0

user1228
user1228

Reputation:

  1. The variable of the DependencyProperty is marked readonly; in .NET that means only the variable is read only. The state of the object referenced by the variable can still be changed. For more info about how readonly works, see this MSDN article.

  2. When a DependencyProperty is set, it requires two things: The value of the property, and the instance of the owning type that the property is to be set on. The DependencyObject/DependencyProperty system manages which instances have what values assigned to them.

The DependencyProperty is static to the class so that there is one single DependencyProperty instance responsible for managing the state of a DP for all instances. As to why this is requires a lot of explanation about DependencyObjects.

Upvotes: 2

Related Questions