Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PropertyDescriptor and WPF Binding mechinsm

Tags:

c#

wpf

xaml

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

like image 728
Omri Btian Avatar asked Nov 05 '13 07:11

Omri Btian


People also ask

What is ItemsSource binding WPF?

ItemsSource can be data bound to any sequence that implements the IEnumerable interface, although the type of collection used does determine the way in which the control is updated when items are added to or removed.

What is WPF data binding?

Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.

What is binding path in WPF?

{Binding Name.Length} Bind to the Length property of the object in the Name property of the current DataContext. {Binding ElementName=SomeTextBox, Path=Text} Bind to the “Text” property of the element XAML element with name=”SomeTextBox” or x:Name=”SomeTextBox”.


1 Answers

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. :)

like image 155
dev hedgehog Avatar answered Oct 15 '22 02:10

dev hedgehog