Reputation: 6378
I have a setup something like below:
MainWindow ------> MainWindowViewModel
|--Menu
|--Frame
Page1 --------> Page1ViewModel
Page2 --------> Page2ViewModel
Suppose my menu has 2 items in it namely Item1 and Item2. Menu's SelectedMenuItem is bound to a property called SelectedItem in MainWindowViewModel. When I click on Item1 I am navigated to Page1 and similarly when I click on Item2 I am navigated to Page2.
Now, In Page1ViewModel and in Page2ViewModel I want to have the current value of SelectedItem. How can I get that value ?
Before asking this question I have looked at Mediator Pattern as well as EventAggregator but for a newbie(in WPF) like me it is very hard to understand those patterns. Is there any other solutions that I can adopt?
Also I have looked at the accepted answer of this question. I have tried the code given there but I don't get value in Page1ViewModel or Page2ViewModel.
Update ---> @Poke
Sorry, I didn't mentioned that in question.
I have another item in my menu called Item3. When I Click on it Page1 should be visible.
So, now my setup is:
MenuItem | Page | ViewModel
-----------+---------------+-------------
Item1 | Page1 | Page1ViewModel
Item2 | Page2 | Page2ViewModel
Item3 | Page1 | Page1ViewModel
The only difference between Item1 Click and Item3 Click is that :
When Clicking on Item1 :
A Combobox called cbEffects should be visible.
When Clicking on Item1 :
A Combobox called cbEffects should be hidden.
Upvotes: 2
Views: 2045
Reputation: 909
Not sure if this is what you are looking for, but you can access the application viewmodel through the App.Current.MainWindow.DataContext property, and store common properties in there.. or, what I do, is store variables accessible to all views in a static class..
Upvotes: 0
Reputation: 3655
Here's one example of maintaining ViewModel instances in a static property available anywhere in your application via SessionViewModel.GetModuleInstance('VMName')
:
namespace MyProject.ViewModels
{
public sealed class SessionViewModel : ViewModelBase
{
private static readonly SessionViewModel instance = new SessionViewModel();
public static SessionViewModel Instance
{
get { return instance; }
}
private List<IModule> modulesOpen;
public List<IModule> ModulesOpen
{
get { return modulesOpen; }
set
{
modulesOpen = value;
NotifyPropertyChanged(() => ModulesOpen);
}
}
}
public static IModule GetModuleInstance(string moduleName)
{
string finalName = "MyProject.ViewModels." + moduleName + "ViewModel";
IModule moduleToOpen = null;
if (Instance.ModulesOpen != null)
{
moduleToOpen = Instance.ModulesOpen.SingleOrDefault(mod => mod.ModuleName == moduleName);
}
else
{
Instance.ModulesOpen = new List<IModule>();
}
if (moduleToOpen != null) return moduleToOpen;
Type module = Type.GetType(finalName);
moduleToOpen = (IModule) Activator.CreateInstance(module);
Instance.ModulesOpen.Add(moduleToOpen);
return moduleToOpen;
}
public class UsageExample()
{
var vm = SessionViewModel.GetModuleInstance("MyVMName");
((MyVMName)vm).MyVMPropertyName;
}
}
Upvotes: 1