Adventures in MVVM – My ViewModel Base – Silverlight Support!

More Adventures in MVVM Shout it kick it on DotNetKicks.com

EDIT: Here is why I love blogging and sharing code so much: Putting your ideas and code out into the public space always manages to add value to the ideas that were originally posted.  Case in point: Tobias Richling commented on this post with a fantastic refinement to the Silverlight dynamic property binding.  There is a much more simple way to bind against these dynamic properties (including automatic commanding) than my first approach.  I have incorporated the changes to the CodePlex site and I have edited this post to reflect the changes.  Thanks again, to Tobias for the suggestion.

In my last post, I outlined the powerful features that are available in the ViewModelSupport.  It takes advantage of the dynamic features of C# 4.0 (as well as some 3.0 goodies) to help eliminate the plumbing that often comes with writing ViewModels.  If you are interested in learning about the capabilities, please take a look at that post and look at the code on CodePlex.

When I wrote about the ViewModel base class, I complained that the features did not work in Silverlight because as of 4.0, it does not support binding to dynamic properties.  Although I still think this is a bummer, I am happy to say that there is a workaround.  In the Silverlight version of my base class, I include a string indexer for getting and setting properties that lets you bind to dynamic properties in the ViewModelBase, especially the convention-based commands that the base class supports.

For example, with a View Model that looks like this:

public class ExampleViewModel : ViewModelBase
{
    public void Execute_MyCommand()
    {
        Set("Text", "Foo");
    }
}

The view can bind to the dynamic property (Text) and the convention-based command (MyCommand) with the following XAML.

<TextBlock Text="{Binding [Text]}" Margin="5" />
<Button Content="Execute MyCommand" Command="{Binding [MyCommand]}" Margin="5" />

Notice the square brackets around the name of the property.  Of course, it is not as perfect as binding to Text and MyCommand like you can in WPF, but it is better than having a failed feature.  This allows you to share your ViewModels between WPF and Silverlight very easily.

<BeatDeadHorse>Hopefully, in Silverlight 5.0, we will see binding to dynamic properties more directly????</BeatDeadHorse>

Tags: , , ,

5 Responses to “Adventures in MVVM – My ViewModel Base – Silverlight Support!”

  1. Hello Brian,

    thank you for mentioning my SL4 binding idea.

    I think you have a type in your code example as it shows the old code in my oppinion.

    Instead of:

    it should be:

    Still I am using lots of your initial ideas in our code and i like it very much. It saves me of lots of glue code.

    Thank you and greetings,

    Tobias.

  2. Outstanding read, I just handed this onto a friend who was doing a little analysis on that. And he in fact bought me lunch because I found it for him…. smile.. So let me rephrase that: Thanks for lunch!But yeah Thank you for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

  3. Andrew says:

    Brian,

    Thanks for sharing your code with us. I was curious if you have a work around for Binding Dynamic properties with a DataForm in Silverlight.

    Andrew

  4. Andrew says:

    Brian,

    Everything is cool with binding dynamic properties to a dataform. I had some other issues going on. It’s working well and I love the shortened code for the properties and commands!

    Andrew

  5. Rob says:

    Congratulation…..you are a legend and have made my experience of MVVM a little more enjoyable! Thanks.

Leave a Reply