Dec 7, 2010 at 9:10 PM
Edited Dec 7, 2010 at 9:10 PM
I am a bit unsure how to bring a querystring value from the view into the view model.
I am so far using a very simple method of specifying the viewmodel as the datacontext of the view.
In my situation I have a ModuleFrameView that has a menu control on it. The menu is populated from data returned from a RIA data service. There really is no easy way to build the UI control in the viewmodel (or is there) so I am building it in the code behind.
The ModuleFrameView is reused based and the module name is passed to it. The moduleId is passed to it in the query string. All of the code to get data from the service (or just build some sample data if in design mode) is in the view. I can see a way to
get the querystring data in the viewmodel.
So what I did was expose a method in the view model called LoadMenus that accepts the model id and also the collection of entities. I also exposed an event that is raised once the RIA service returns the data. In the code behind of the View I cast the Datacontext
to the ViewModel type, assign a handler to the MenusLoaded event and then call the menus loaded passing in the value from the query string. (This is done in the OnNavigatedTo event handler, I'm using basic SL4 navigation not prism regions).
This all seems to work but is also seems wrong and brittle. Is there a better way to get the querystring data in the view model?
It seems to me that there would be a lot less event gymnastics if I just did everything in the code behind. If there is a way to build the menu control in the viewmodel I just don't see it. Ingragistics has not been able to get me a sample of binding the
menu to a property in the viewmodel.