This project has moved. For the latest updates, please go here.

Binding View Models to Views

In Jounce, binding a view model to a view is easy. Anywhere in the application, you simply need to export a ViewModelRoute. The route is passed a view model and a view, and Jounce takes care of the rest.

For example, to bind the view model tagged "Calc" to the view tagged "Shell" you do this:

[Export]
public ViewModelRoute Binding
{
    get
    {
        return ViewModelRoute.Create("Calc", "Shell");
    }
}    

If you are exporting views and view models usingly typed names, the routes can be created using the types as well, like this:

[Export]
public ViewModelRoute Binding
{
    get
    {
        return ViewModelRoute.Create<CalcViewModel,ShellView>();
    }
}    

You may also perform an explicit, runtime binding like this:

[Import]
public IFluentViewModelRouter Router { get; set; }

public void SomeMethod()
{
   Router.RouteViewModelForView("Calc","Shell");
}


If you export views and view models using types, you can use the type-safe version like this:

[Import]
public IFluentViewModelRouter Router { get; set; }

public void SomeMethod()
{
   Router.RouteViewModelForView<CalcViewModel,ShellView>();
}



That's it! A view model may be bound to multiple views.

Last edited Mar 27, 2011 at 8:05 PM by jeremylikness, version 5

Comments

No comments yet.