Geeks With Blogs

News Locations of visitors to this page
Brian Genisio's House of Bilz

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>

Posted on Friday, May 14, 2010 2:24 PM | Back to top


Comments on this post: Adventures in MVVM – My ViewModel Base – Silverlight Support!

# re: Adventures in MVVM – My ViewModel Base – Silverlight Support!
Requesting Gravatar...
Hi Brian,

cool stuff that you have posted so far. Thanks for sharing.

I adopted some of your code for my own purposes, but I was not happy using the value converter for command binding, while I can live with defining all my properties at design time. Due to this, the commands are the only thing that I want to bind dynamically.

I realised this with another new Silverlight 4 feature, which is to bind against a dictionary using an indexer in the view model class.

I defined an indexer accessing the _values dictionary like so:

public object this[string key]
{
get
{
if (_values.ContainsKey(key))
return _values[key];
return null;
}
set
{
if (!_values.ContainsKey(key))
_values.Add(key, value);
_values[key] = value;
}
}

All other code for command creation remains the same.

I can than bind to the command in xaml this way:

<Button Content="Click Me" Command="{Binding [ButtonClick]}">

Using this binding syntax I can even bind against properties that are dynamically defined at runtime such as with

Set("SomeDynamicProp", "Foo");

can be bound as

<TextBox Text="{Binding [SomeDynamicProp], Mode=TwoWay}">

I have not tested this with your DependsUpon stuff - I acutally did not test this very much at all, but my basic experiments worked like a charm.

Perhaps you may find this useful.

Tobias.
Left by Tobias Richling on May 17, 2010 9:00 PM

# re: Adventures in MVVM – My ViewModel Base – Silverlight Support!
Requesting Gravatar...
Tobias,

That is some awesome, awesome sauce. I have incorporated your changes and pushed them to CodePlex. I will update the blog post very soon.

It turns out that this is the code you need to make everything work:

public object this[string key]
{
get { return Get<object>(key);}
set { Set(key, value); }
}

Also, when the property changes, you need to fire a INotifyPropertyChanged event with an empty string, in order to get two-way binding:

PropertyChanged.Raise(this, "");

Thank you VERY much for this refinement. Instead of a big hack, it is only a small hack. You rock!

Brian
Left by Brian Genisio on May 18, 2010 1:49 AM

# canlı maç izle,maç izle,lig tv izle,bedava lig tv izle
Requesting Gravatar...
canlı maç izle,maç izle,lig tv izle,bedava lig tv izle
Left by tribundeyim on Jan 27, 2011 3:52 AM

Your comment:
 (will show your gravatar)


Copyright © Brian Genisio's House Of Bilz | Powered by: GeeksWithBlogs.net