I usually have a property in my ViewModel like this:
public DependencyObject Owner { get; set; }
Now when I pass the ViewModel into the view’s ctor, I let the ViewModel know
who “owns” it:
public ProductsPage(ProductsViewModel viewModel)
{
this.DataContext = viewModel;
viewModel.Owner = this;
}
Once the viewModel is wired back to the owner, you can use
NavigationService.GetNavigationService(this.Owner) from the ViewModel to get a
handle to the page’s NavigationService, which will let you navigate to the next
view.
In terms of spawning another view, I would take a dependency on whatever views
you need as factory lamdas. For example:
public ProductsPage(ProductsViewModel viewModel, Func<IProductDetailView>
detailViewFactory) { ... }
Now you can call detailViewFactory() and have it return (for example) an
instance of ProductDetailPage, which you can navigate to.
Matt
From: Winston Pang
Sent: Wednesday, November 03, 2010 7:02 PM
To: ozDotNet ; ozWPF
Subject: MVVM in a navigational paradigm
Hey guys,
I'm trying to apply MVVM in the WPF navigation model.
I was just doing some thoughts around it
Apart from the rule that the view model shouldn't know about the view, how
would a particular view spawn another view, and push it to the navigation
service for example? I've been playing around with some ideas of holding a
mapping between the View and ViewModel in a global list in App. Then have App
register against the messenger/mediator to respond to any other view model's
wanting to spawn a new view and navigating it to it. I'm not sure if I'm on the
right track.
Would love to see how some other people have done it on here?
Thanks.
--Winston
--------------------------------------------------------------------------------
_______________________________________________
ozwpf mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf
_______________________________________________
ozwpf mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf