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

Tuesday, 8 June 2010

Automatic INotifyPropertyChanged with DynamicObject

Implementing the INotifyPropertyChanged interface is Microsoft’s recommended mechanism for providing notifications to update WPF data bindings.

Normally, I like to use my code snippets to implement INotifyPropertyChanged on classes I’m writing, but what if I want to use data binding on a class that I didn’t write and which doesn’t support INotifyPropertyChanged?

In this post, I show how to use the DynamicObject class introduced in .Net 4 to create a proxy that wraps access to another object’s properties while adding automatic support for INotifyPropertyChanged.

Wednesday, 2 June 2010

Snoop is on CodePlex

Snoop is an essential tool for understanding and debugging WPF applications.

snoop

Originally created by Pete Blois, Snoop is now maintained by Cory Plott, who has made it available on CodePlex, and added WPF 4.0 compatibility.

Simply put, Snoop displays the visual tree of a running WPF application in the left pane, and the properties of the selected element in the right pane. Read a more detailed description from Pete Blois at http://blois.us/Snoop/

Download the latest Snoop from http://snoopwpf.codeplex.com/releases