If you work with Silverlight and MVVM, you must have probably been forced to do some weird thing to bind to a property in your view model (say a Command) from a DataTemplate. Some of the common approaches I have seen and used myself are:
- Using some kind of binding helper as a View’s resource.
- Having a View Model wrap the entity of each collection item, and add the command to the View Model.
Although these are perfectly valid approaches, they require extra work compared to the good old {Binding} syntax we are all used to.
Now that Silverlight 5 Beta is out, we can take advantage of the XAML Markup Extensions to help is in this kind of tasks, using a simpler and more intuitive approach.
I have created a RootBindingExtension, which can be used as follows:
The RootBindingExtension uses the Path property to create a Binding that binds to a property of that name in the View’s DataContext.
You can download the RootBindingExtension and a sample that uses it from here.
Let me know if you have used it.