WPF Prism V2 Using MV-VM - Adding a Runtime View to a Region from a ViewModel
Hopefully pretty simple, with my first try at WPF with Prism V2 using MV-VM and it's pretty easy to find so far. My wrapper is pretty simple: Ribbon Control on top, DataGrid from Help on the left, and TabControl on the right.
When the user opens the selected ticket from the datagrid, I want the Ticket to open as a tab in the Tab control. I know for this I need to add and then activate the view in the region using the RegionManager. But doing it from the ViewModel doesn't seem right to me, although I could do it with DI (DepenecyInjection), it still rings my head to give the ViewModel some view details.
To add to this, the various modules will also add other views (Contact, Client, etc.) to the TabControl, I would like to use DataTemplates to make the TabControl display the View Correctly, can anyone give me any pointers to it's the same.
Thanks a lot Ben
Full answers please, not just links. What is StackOverflow for!
a source to share
MVVM + Services = Ultimate Power!
A service is simply an interface that is well known and registered with your IOC container. When the ViewModel has to do something outside of itself, like, say, open a tabbed document, it uses this service. The service is then implemented as needed for a specific program.
For instance:
public interface IDocumentService
{
void OpenDocument(IViewModel viewModel);
}
internal class DocumentService:IDocumentService
{
public void OpenDocument(IViewModel viewModel)
{
// Implement code to select the View for the ViewModel,
// and add it to your TabControl.
}
}
{
// Somewhere in your ViewModel...
// Make sure you can get the IDocumentService
IDocumentService docService = ioc.Get<IDocumentService>();
docService.OpenDocument(new TicketViewModel());
}
a source to share
Commands are the way to do this - you send a command to yourself called "RequestBringTicketIntoView"; it will turn into the window where you handle it. Read Josh Smith's article:
http://joshsmithonwpf.wordpress.com/2008/03/18/understanding-routed-commands/
a source to share