One useful feature in WPF 4.0 is the ability to databind to dynamic (runtime generated) properties using the DynamicObject as a base class or implementing the IDynamicMetaObjectProvider interface. I am currently working on a WPF application and this ability to bind to runtime generated properties would have been very useful in a previous story we implemented two weeks ago.
The story concerned merging two object graphs and then visualizing what properties that were changed/conflicted in the UI (for example with a different color).
In order to not add “XXX_HasMergeChange” for every property in the presentation model we solved this by using a value converter and some WPF binding magic that some might call a HACK. The solution was only partial as it only worked in the Grid and not on everything else.
If we had WPF 4.0 we could have solved this like this:
In the above style trigger the data trigger is binding to a property that doesn’t exist on the presentation model. How does WPF then get the value for this property? By calling the TryGetMember method:
WPF will call the TryGetMember and that will check if the property ends with “_HasMergeChange”, if that is the case it will try to look up the property in the MergeChanges dictionary.
The above is just a simple proof of concept, if I would go forward with this I would have to figure out a more generic way to define the style and data trigger to be able to reuse the xaml style markup for example, but that shouldn’t be a big problem. I also tested property change notifications using the INotifyPropertyChanged interface and they work for dynamic properties as well.
To learn more about the new features in WPF 4.0 read ScottGu’s recent post.