My view model base
My view model base - Silverlight support
To me, this base class contains of two basic ideas.
- The first idea is that you do not need to write all those ICommand-type properties yourself but let them generate dynamically from methods that are named by the convention Execute_< commandname > / CanExecute_< commandname >. The properties are stored within a dictionary within the base class.
- The second idea is not to write all these OnPropertyChanged notification code all the time you publish a property in your view model, but let the base class do it. For this reason the base classes dictionary (the same on in which the ICommand-properties are stored) acts as a value storage for all your property values. Within your view model you only call a base class method to get and set the value. Therby all get /set code runs thorugh a central point that handles the OnPropertyChanged-stuff.
Up to Silverlight 3 there was a problem with data binding of these "dynamic" properties, because in contrast to WPF Silverlight was not able to bind against dictionaries. The workaround was to use a value converter to do the magic.
With Silverlight 4 it is possible to bind against indexers using the following syntax:
< button type="submit" command="{Binding" content="Click me" />
Note the square brackets around the command name - they indicate that the name inside the brackets is to be interpreted as the key of the indexer and yields to
MyViewModelInstance["ButtonClick"]
With this new feature in place, the value converter is no longer needed, which makes the code more readable.
Brian hosted all the sourcecode on codplex.
Thanks for sharing!
Keine Kommentare:
Kommentar veröffentlichen