Thursday 17 June 2010

Data binding to Fields with a DynamicObject Proxy

WPF data binding only works with properties, not fields. If you have an existing type with fields that you want to bind to, you need some kind of wrapper to expose the fields as properties. That can lead to a lot of replication, wouldn’t it be nice if there was an automatic way to do this?

Enter FieldToPropertyProxy. This class is a slight variation on my previous post, again derived from DynamicObject.

The implementation is almost identical to NotifyPropertyChangedProxy, except that references to GetProperty and PropertyInfo are replaced with GetField and FieldInfo.

As well as converting fields into properties, NotifyPropertyChangedProxy includes an implementation of INotifyPropertyChanged.

Download the code here: FieldToPropertyProxy.cs

No comments:

Post a Comment