开发者

WPF App With several Usercontrols passing values to MainViewModel (MVVM)

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.C开发者_运维知识库olumn="1"/>

any help would be appriciated Greatly.

Thanks.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜