Omri Btian
Omri Btian

Reputation: 6547

PropertyDescriptor and WPF Binding mechinsm

Background

I am investigating some code and encountered a xaml containing a DataGrid with some binded columns:

Binding="{Binding calc_from}" ....

I've search everywhere but there is no class containing a property called calc_from. Then I stumbled upon some PropertyDescriptor classes. I figured that this is how they make the binding work but didn't fully understand how.

Question

What is PropertyDescriptor and what is it good for? When will I want to implement my own CustomTypeDescriptor? And how does it relate to WPF binding mechanism?

I've came accross an example in this thread but I'd be happy if someone will shed some light on it

Upvotes: 9

Views: 4066

Answers (1)

dev hedgehog
dev hedgehog

Reputation: 8791

What is PropertyDescriptor and what is it good for?

PropertyDescriptor is an abstract class providing few methods and properties which are mostly used in Binding class internally. For example WPF has those "normal" properties and the dependency properties and so does Binding use a PropertyDescriptor for the normal ones and a DependencyPropertyDescriptor which inherits from PropertyDescriptor and overrides its abstract methods such as SetValue, GetValue, ResetValue..etc. Futhermore those PropertyDescriptors provide a mechanism to listen to PropertyChanged events if the owner class of the actual property has INotifyPropertyChanged implemented. To sum up when we talking about Bindings in WPF then there is PropertyDescriptor on one side against the model class providing that desired property on another side.

When will I want to implement my own descriptor?

The only example I can think of right now is when you cannot implement INotifyPropertyChanged in your entity class for whatever reason and you have to do some kind of polling to ask or change a property then you will write your own PropertyDescriptor doing kind of polling on a property asking for its value every 1/100 for a second. If you tell the Binding to use your custom PropertyDescritor you will end up having a "PollingBinding" class.

Another example is "DelayBinding" that some guys wrote here on the internet having a custom PropertyDescriptor in combination with Binding which counts how often you wished to set a value on a property and if you trying to set the value 1000 times in 1/100 of a second then that thing will allow you to do so though every 10th time and so it will provide you a small delay.

The example from the link you posted us is another good example. In that question the guy wished to have its own custom type descriptor managing its own custom change notifications. That is where PropertyDescriptor comes handy.

PropertyDescriptor works usually with Binding. That thing alone is pretty dump. :)

Upvotes: 6

Related Questions