Yesterday I blogged about MVVM for WindowsForms and I mentioned Truss, the databinding library à la WPF that is UI independent. Note I said independent, not agnostic. In fact, it can bind to:
- an ordinary property contained in an object that implememts INotifyPropertyChanged
- a DependencyProperty of a DependencyObject
- an ordinary property of a WindowsForms control
Truss implements a binding strategy that uses the Microsoft PropertyNameChanged Pattern. Given a property Address, Truss attaches itself to the AddressChanged event. It happens properties of WinForms controls raise this event instead of the PropertyChanged(<propertyname>) event that is raised by INotifyPropertyChanged properties.
A word about binding expressions
In our example, we have:- MainForm - the view with two text boxes: txName and txAddress
- MainFormViewModel - the ViewModel that contains a Model property of type MainFormModel
- MainFormModel - the model with two string properties: Name and Address
- using a simple string path expression
- using a lambda expression
If you are going to inject your bindings using convention over configuration, you must use the first variation.
You might be tempted to refer the root object and use a multipart path.
Well don't. This doesn't work. I'm not sure whether this should work under Truss. I intend to come back on this subject.
As a closing subject, on my quest for MVVM for WindowsForms I found another interesting piece of MVVM stuf. Magical.Trevor implements convention over configuration to find a View for a given ViewModel and bind both together. The same pattern is also used to self-bind:
Listing 1 - Single part path
You might be tempted to refer the root object and use a multipart path.
Listing 2 - Multi part path
Well don't. This doesn't work. I'm not sure whether this should work under Truss. I intend to come back on this subject.
As a closing subject, on my quest for MVVM for WindowsForms I found another interesting piece of MVVM stuf. Magical.Trevor implements convention over configuration to find a View for a given ViewModel and bind both together. The same pattern is also used to self-bind:
- Button Click event to methods
- TextBox Text property to string properties
I tries Truss. When I change my object, TextBox updates OK, but when I change textBox.Text (by typing or by code) it does not update my object. I think this is because TextBox does not implement INotifyPropertyChanged. Am I forgetting something? Any workarounds?
ReplyDeleteHi Mohammad,
ReplyDeleteI'm not using Truss any more. The Codeplex project http://mvvmfx.codeplex.com/ includes the features of Truss. MVVM FX also includes some smal code samples.