Use MVVM Light’s Messenger to Pass Values Between View Model

Say I have two ViewModels and a ViewModelLocator. I want to be able to pass parameters between all three without issue. How would I go about doing this with the messenger? Is it capable of that? That’s exactly what it’s for, yes. To send a message: MessengerInstance.Send(payload, token); To receive a message: MessengerInstance.Register<PayloadType>( this, token, … Read more

MVVM light – how to access property in other view model

You could use the Messenger to do this: Send the user in the UserViewModel: Messenger.Send<User>(userInstance); would just send the user to anyone interested. And register a recipient in your CardViewModel: Messenger.Register<User>(this, delegate(User curUser){_curUser = curUser;}); or you can also send a request from your CardViewModel for shouting the user: Messenger.Send<String, UserViewModel>(“Gimme user”); And react on … Read more

Handling the window closing event with WPF / MVVM Light Toolkit

I would simply associate the handler in the View constructor: MyWindow() { // Set up ViewModel, assign to DataContext etc. Closing += viewModel.OnWindowClosing; } Then add the handler to the ViewModel: using System.ComponentModel; public void OnWindowClosing(object sender, CancelEventArgs e) { // Handle closing logic, set e.Cancel as needed } In this case, you gain exactly … Read more

MVVM Light RelayCommand Parameters

I believe this will work: _projmenuItem_Edit = new RelayCommand<object>((txt)=>ProjEditNode(txt)); — EDIT — You’ll need to define your RelayCommand with the type as well: e.g. public RelayCommand<string> myCommand { get; private set; } myCommand = new RelayCommand<string>((s) => Test(s)); private void Test(string s) { throw new NotImplementedException(); }

Adding controls dynamically in WPF MVVM

If you really want to do mvvm , try to forget “how can I add controls”. You don’t have to, just think about your viewmodels – WPF create the contols for you 🙂 In your case lets say we have a SearchViewModel and a SearchEntryViewmodel. public class SearchEntryViewmodel { //Properties for Binding to Combobox and … Read more

How to open a new window using MVVM Light Toolkit

Passing a message from ViewModel1 to View1 means to use the messaging capabilities in the MVVM Light Toolkit. For example, your ViewModel1 could have a command called ShowView2Command, then it would send a message to display the view. public class ViewModel1 : ViewModelBase { public RelayCommand ShowView2Command { private set; get; } public ViewModel1() : … Read more