1

In my project I have a TitleView and GameView. When the program launches, TitleView is displayed. The user clicks a button and GameView is displayed. I am using MVVM Light which includes MainViewModel which has commands to switch to the desired views:

From MainViewModel.cs

GameViewCommand = new RelayCommand(() => ExecuteGameViewCommand());

private void ExecuteGameViewCommand()
{
    CurrentViewModel = MainViewModel._gameViewModel;
}

In TitleView.xaml, I need to access this command and I don't know how. I am very much a novice when it comes to XAML.

From TitleView.xaml

<UserControl x:Class="AoW.Views.TitleView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:vm="clr-namespace:AoW.ViewModels"
         mc:Ignorable="d" 
         d:DesignWidth="1020"
         d:DesignHeight="740" 
         Width="1020"
         Height="740">

    <Button Content="New Game"
            //This needs to bind to GameViewCommand in MainViewModel.cs
            Command="{Binding GameViewCommand}"
            Grid.Column="1"
            Grid.Row="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />

If I put the following line into TitleView.xaml...

DataContext="{Binding Main, Source={StaticResource Locator}}"

... I can access GameViewCommand, but I can't access any local commands.

What can I do to gain access to GameViewCommand while maintaining control of local commands?

If providing additional code would be helpful, please let me know.

Jason D
  • 2,634
  • 6
  • 33
  • 67
  • You have bound the command property of the Button to your GameViewCommand. Where else in the Xaml do you need access to it? – Gayot Fow Jun 02 '13 at 20:25
  • `GameViewCommand` is in `MainViewModel`. I would like to find a way to bind that command to a button in `TitleView`. Is this possible? – Jason D Jun 02 '13 at 21:05
  • Did you consider refactoring it into your BaseViewModel as a static member and siting the processing logic there? – Gayot Fow Jun 02 '13 at 21:20
  • I got MVVM Light from NuGet and though the ViewModels inherit from `ViewModelBase`, it's not actually included in the project. I don't know how I would go about finding it. Also, look at the bottom of my post; I just updated it with some information that may be valuable. If there is more code that might be helpful, please let me know so that I can post it. – Jason D Jun 02 '13 at 21:33
  • Can you create a MyBaseViewModel which inherits from ViewModelBase and implements what you need and have your ViewModels inherit from that instead? – Gayot Fow Jun 02 '13 at 21:40
  • I have to leave the house for awhile. I'll look into this when I get back. So there's really no direct way for one View to use a command from another ViewModel? While it probably shouldn't happen often, it seems like it should be possible. – Jason D Jun 02 '13 at 21:50
  • It can also be done in the section, but this is actually worse. Consider the "S" in SOLID. – Gayot Fow Jun 02 '13 at 21:56

1 Answers1

0

I've fixed the problem. All I had to was this:

<Button Content="New Game"
            Command="{Binding GameViewCommand}"
            DataContext="{Binding Main, Source={StaticResource Locator}}"
            Grid.Column="1"
            Grid.Row="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />

<Button Content="Say Hello"  
            Command="{Binding SayHelloCommand}"               
            Grid.Column="2"
            Grid.Row="2"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />

Turns out it was a very easy fix.

Jason D
  • 2,634
  • 6
  • 33
  • 67
  • Do you know why this works? I'm quite confused as the datacontext is already defined as "{Binding Main, Source={StaticResource Locator}}" in App.xaml's resources. – Clement Hoang Aug 05 '13 at 16:25
  • If I recall correctly, TitleView.xaml was linked to TitleViewModel via a DataTemplate in App.xaml. GameViewCommand was in MainViewModel and by default wasn't accessible by TitleView. Overriding the DataContext of the button allowed me to bind to the command in MainViewModel. – Jason D Aug 05 '13 at 17:11