1

I am new to MVVM and working on an application, i want to achieve few things in my application

  1. My viewmodel should be able to initiate a new view. scenario(a command is bind to a button and some process decide what to do and based on the result, i need to show View1 or View2)

  2. Upon a successfull operation my viewmodel should display a messagebox, if multiple views are open then message must prompt upon the right view(with which viewmodel is bind).

I want to provide some kind of notification from my view model to view. Kindly guide me in the right direction.

Thanks

manav inder
  • 3,531
  • 16
  • 45
  • 62

2 Answers2

1

You might want to try out some of the many mvvm frameworks out there. I personally like mvvm light because it works in silverlight and WPF, and it's easy to use http://mvvmlight.codeplex.com/ (no affiliation)

Here is a nice compare/contrast of some of the major frameworks: What framework for MVVM should I use?

Most of the frameworks have a messaging system that provides the ability to send updates between the view and the viewmodel as well as between viewmodels. Most of the frameworks also provide canned messages that handle MVVM messageboxs as well (I know MVVM Light does).

To handle switching between views in WPF I use DataTemplates and Content controls

In the view .xaml I add <ContentControl Content="{Binding ActiveViewModel}" /> and this is the space where the injected view will be displayed. The ActiveViewModel is the object for the viewModel that holds the selected viewModel.

In a ResourceDictionary I add something like:

  <DataTemplate DataType="{x:Type ViewModelNameSpace:ViewModelClassName}">
    <ViewNameSpace:ViewClasName/>
  </DataTemplate>

Finally in the ViewModel I set the ActiveViewModel property (that is setup to notify the UI of changes via INotifyPropertyChanged) to an instance of the viewModel I would like to use.

ActiveViewModel = new ViewModelClass();
Community
  • 1
  • 1
Erick
  • 486
  • 2
  • 12
0
  1. You should create a new View and Navigate to it.
  2. You can use messaging in MVVM Light framework. Send message from your ViewModel to View. Examples: http://chriskoenig.net/2010/07/05/mvvm-light-messaging/
iJoy
  • 87
  • 10