Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM Light: how to send a parameter to ViewModel (when opening new window)?
    primarykey
    data
    text
    <p>I am trying to learn MVVM with MVVM Light Toolkit in WPF. But I am stuck on one simple problem. </p> <p>I have an AddEditProfileWindow which basically has a textbox for profile name and a confirm button. It adds new profile to database table or updates name of existing profile. </p> <p>In MainWindow/MainViewModel I have a list of profiles and two buttons: "Add Profile" and "Edit Selected Profile". They both open this window via commands + messages. For example here is command for the "Add Profile" button</p> <pre><code> public RelayCommand OpenAddProfileWindowCommand { get { return _openAddProfileWindowCommand ?? (_openAddProfileWindowCommand = new RelayCommand( () =&gt; { Messenger.Default.Send(new NotificationMessage("OpenAddProfile")); })); } } </code></pre> <p>and it's receiver in MainWindow code behind</p> <pre><code>private void MessageReceived(NotificationMessage msg) { if (msg.Notification == "OpenAddProfile") { var window = new AddEditProfileWindow(); window.Owner = this; window.ShowDialog(); } } </code></pre> <p>So the problem is that I need to somehow pass a parameter to the AddEdit... Window/ViewModel (set IsEditing bool property in ViewModel for example) to change window behavior and customize it a bit (change title and the confirm button text to "Add" or "Update"). Also for updating I need Profile object (or at least Id) of selected record.</p> <p>For creating ViewModels I use ViewModelLocator and Unity</p> <pre><code> public ViewModelLocator() { var container = new UnityContainer(); ServiceLocator.SetLocatorProvider(() =&gt; new UnityServiceLocator(container)); container.RegisterType&lt;MainViewModel&gt;(new ContainerControlledLifetimeManager()); // singleton container.RegisterType&lt;AddEditProfileViewModel&gt;(); } public AddEditProfileViewModel AddEditProfile { get { return ServiceLocator.Current.GetInstance&lt;AddEditProfileViewModel&gt;(); } } </code></pre> <p>I have read a lot of similar threads and examples but still don't understand how should I pass parameters to view models. Some answers suggest creating view models on app startup (and make them singletons) in the ViewModelLocator and then I can send message before opening. But looks like not very clean and also I will need to reset view models before opening (via Cleanup() probably).</p> <p>Is there any better/easier/cleaner approach? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload