Calling another viewmodel's RelayCommand from within MainViewModel

Sep 10, 2013 at 7:52 PM
Edited Sep 10, 2013 at 8:17 PM
Hi, This is pretty simple, but being utterly new to wp8 and mvvmlight, I'm not figuring this out.

To put simply:
On my MainPage.xaml, on the application bar, I have a New Task button. This is bound to a Command:
<behaviors:ApplicationBarIconButton Command="{Binding SaveNewTaskCommand}"
But the SaveNewTaskCommand is in my NewViewModel.cs:
public RelayCommand SaveNewTaskCommand
        {
            
            get { return new RelayCommand(SaveNewTask); }
        }
( .. and SaveNewTask simply navigates to my NewPage.xaml via: _navigationService.NavigateTo(new Uri(@"/View/NewPage.xaml", UriKind.Relative)); )

However, nothing happens when I tap the New Task button. I'm guessing because I'm binding to a RelayCommand that's not in the MainViewModel

So I wasn't sure what the syntax is for specifying the correct context of that other ViewModel

Any help is appreciated
Steve

( p.s. I'm using the Cimalino toolkit for the appbar binding )
Sep 11, 2013 at 11:15 AM
Edited Sep 11, 2013 at 11:16 AM
Can't say I've done any WP8 dev so I'm not 100% sure how your using it.

Is binding a relaycommand to a context outwith your viewmodel is a bit anti mvvm pattern? Not 100% sure on that one.

It might be better the use the messenger class, then in the viewmodel of your mainpage.xaml view you could send a message and as long as you subscribe to that message type in the NewViewModel class you can trigger your new task.

A plan B anyway.
Coordinator
Sep 11, 2013 at 12:17 PM
Hi,

You can bind to any ViewModel as long as it is reachable. The VM can either be exposed through the ViewModelLocator in which case you can use:

MyCommand="{Binding TheViewModel.TheCommand, Source={StaticResource Locator}}"

or it could be exposed as a property of the MainViewModel (for instance), in which case you can do

MyCommand="{Binding TheViewModel.TheCommand}" if MainViewModel is already the page's datacontext or
MyCommand="{Binding Main.TheViewModel.TheCommand, Source={StaticResource Locator}}"

So basically it's all a matter of finding the reference to the VM that contains the RelayCommand. Note that Blend helps you a lot there (or the Visual Studio designer) with its Binding editor.

Cheers
Laurent
Sep 11, 2013 at 12:23 PM
lbugnion wrote:
Hi,

You can bind to any ViewModel as long as it is reachable. The VM can either be exposed through the ViewModelLocator in which case you can use:

MyCommand="{Binding TheViewModel.TheCommand, Source={StaticResource Locator}}"

or it could be exposed as a property of the MainViewModel (for instance), in which case you can do

MyCommand="{Binding TheViewModel.TheCommand}" if MainViewModel is already the page's datacontext or
MyCommand="{Binding Main.TheViewModel.TheCommand, Source={StaticResource Locator}}"

So basically it's all a matter of finding the reference to the VM that contains the RelayCommand. Note that Blend helps you a lot there (or the Visual Studio designer) with its Binding editor.

Cheers
Laurent
That's cool, I couldn't make that work, must have been doing something wrong :-)
Sep 11, 2013 at 5:16 PM
Perfect Laurent, thank you very much.!