-
Silverlight 4 RC WebBrowser’s lack of binding support (for MVVM)
Posted on April 7th, 2010 2 commentsRecently in a new project powered by Silverlight 4, I’ve begun the shift away from code-behind and into the MVVM world. Honestly I was skeptical and maybe I’ll have a future post explaining my thoughts on it but let’s just say that both Laurent Bugnion’s views on MVVM as well as his excellent MVVM Light Toolkit definitely made me much more comfortable. Since our initial prototype was code-behind, I was able to compare apples to apples to our MVVM prototype and I must say, I’ve had a fun time rewriting my original code to adapt to the bind-ability inspiration of MVVM… for the better I must say.
Anywho, enough about my experiences with MVVM, this post is specifically about the WebBrowser control in Silverlight 4 RC… and to be even more specific, it’s NavigateToString() method. In an MVVM environment where binding is your friend, we’ve got a nice implementation of using the WebBrowser control and binding it to a property in our VM which represents a URL. All fun and games there… but if you want to take advantage of the cool new SL4 NavigateToString() method with proper binding in MVVM, you’re out of luck (at least I think). In order for us to use this method, we used the excellent Messenger class that the MVVM Light toolkit provides which allows our ViewModel to talk to the View.
Just to lay it out… we did this:
- In our ViewModel, on our Set method of our property, we set up message of type string using the Messenger and send the string containing HTML out.
- In our View’s code-behind’s (blech) constructor, we set up the Messenger to receive the string message and call the WebBrowser’s NavigateToString() method on the value.
So we effectively have a datagrid whose SelectedItem is bound to this property so when the user selects an item in the grid, this Messenger is called and the HTML string is sent… we could also easily do this with commanding. The Messenger then receives the HTML string, sets the WebBrowser control to navigate to it, and voila, we have content in our WebBrowser.
Microsoft, please make this functionality bindable in the future where, in our MVVM setup, we can simply bind this in our View to a property in the ViewModel and we’d be set.
FYI – If this is already available or there is a better way to do this, let me know so I can fix my code cause right now I feel violated

