0

I have a wpf app that shows 5 different usercontrols as you go through the system. Each usercontrol has a listbox on it. So i want to select an item and pass it back to MainViewModel. I have it working now so that i can store a value in ViewModelBase but it seems that my tactic for calling the User controls is flawed as I cant link up to each individual ViewModel but only to ViewModelBase. I understand where Im going wrong but I wonder is there a way to do this by initialising each Usercontrol seperately and not just off the viewModelBase as i do here:

    private ViewModelBase _control;
    public ViewModelBase Control
    {
        get { return _control; }
        set
        {
            _control = value;
            OnPropertyChanged("Control");
        }
    } 

and then i say on loaded

     Control = new MainScreenViewModel();
     ynd = new YesNoDelegate(YesNoNavigation);
     Control.SetReturnData(ynd);
     Control.name = "MainScreen";

control is then called in xaml like

     <ContentControl  Content="{Binding Control}" Height="350" Width="525" Grid.Column="1"/>

any help would be appriciated Greatly.
Thanks.

dstuction
  • 3
  • 1
  • I don't know if i'm the only one who didn't understand your design. However can you explain your design clearly? What is meant by this: `So i want to select an item and pass it back to MainViewModel`. You want to select a ListBoxItem? Do all the usercontrol and the mainview share the same datacontext? – Amsakanna Jun 04 '10 at 09:27
  • Well basically i have ViewModels for each usercontrol.So lets say there is usercontrol1.xaml that has a listbox.I have a personVariable in my usercontrol1viewmodel and I store the selected item there.Now i want that to get passed back to my mainViewModel when I click a button.This works when the variables in my ViewModelBase (where i have access to send a delegate) "ynd=new yesnodelegate(yesNoNavigation); Control.SetReturnData(Ynd);" This setReturnData method is in ViewModelBase but i want it in Usercontrol1ViewModel but then it wont access it. Sorry to be confusing. im still learning MVVM – dstuction Jun 04 '10 at 09:41

2 Answers2

0

Take a look at the MVVMLight's Messenger Class if you want communication between the ViewModels.

This particular answer illustrates you how to use it.

Community
  • 1
  • 1
Amsakanna
  • 12,254
  • 8
  • 46
  • 58
0

I switched from using the Messenger Class to using CommandManager which is native to WCF specifically defining a custom RoutedUICommand then using CommandManager.RegisterClassCommandBinding in the location or locations that needed to respond to the command.

The advantage is not having to inherit from mediatorbase Not having to rely on code that the author has stoped maintainng (though it is well written!) Not having to use decorators and rely on strings for the message key