Search code examples
c#wpfmvvmcontentcontrol

Change ContentControl Content from Content ViewModel programmatically


MainWindow.xaml

<Window.Resources>
    <DataTemplate DataType="{x:Type local2:StartPageViewModel}">
        <local1:StartPage/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type local2:SecondPageViewModel}">
        <local1:SecondPage/>
    </DataTemplate>
</Window.Resources>
<DockPanel>
    <StackPanel DockPanel.Dock="Left">
        <Label Content="[res][per]" />
        <Button Command="{Binding CommandStartView}">
            First
        </Button>
        <Button Command="{Binding CommandSecondView}">
            Second
        </Button>            
    </StackPanel>
    <ContentControl x:Name="Pages" DockPanel.Dock="Right" Content="{Binding SelectedViewModel}"/>
</DockPanel>

MainWindowViewModel.cs

class MainWindowViewModel : BaseViewModel
{

    private object selectedViewModel;

    public object SelectedViewModel
    {
        get { return selectedViewModel; }
        set { selectedViewModel = value; OnPropertyChanged("SelectedViewModel"); }
    }

    public ICommand CommandStartView { get; set; }
    public ICommand CommandSecondView { get; set; }

    public MainWindowViewModel()
    {

        CommandStartView = new RelayCommand(openStartView);
        CommandSecondView = new RelayCommand(openSecondView);

    }

    private void openStartView(object obj)
    {
        SelectedViewModel = new StartPageViewModel();
    }

    private void openSecondView(object obj)
    {
        SelectedViewModel = new SecondPageViewModel();
    }

}

StartPage.xaml

<StackPanel>
    <Label Content="First Page" />
    <Button Content="Second" Command="{Binding Path=DataContext.CommandSecondView, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
    <Button Content="Do Something" Command="{Binding CommandDoBeforeSecondView}"/>
</StackPanel>

StartPageViewModel.cs

class StartPageViewModel : BaseViewModel
{
    public ICommand CommandDoBeforeSecondView { get; set; }

    public StartPageViewModel()
    {

        CommandDoBeforeSecondView = new RelayCommand(openSecondView);
    }

    private void openSecondView(object obj)
    {
        Console.WriteLine("DO SOME CODE");

        //Then change Content programmatically

    }
 }

Question: How can I change the Content from the MainWindow ContentControl using the second button in the StartPage? I want to execute some code and then change the content.

In relation of the first comment I think I have to add a reference the MainWindowViewModel in my StartPageViewModel, how i make this?

EDIT

My working Solution:

MainWindowViewModel.cs

private void openStartView(object obj)
{
    SelectedViewModel = new StartPageViewModel(this);
}

StartPageViewModel.cs

class StartPageViewModel : BaseViewModel
{
    private MainWindowViewModel mainWindow;
    public ICommand CommandDoBeforeSecondView { get; set; }

    public StartPageViewModel(MainWindowViewModel _mainWindow)
    {
        mainWindow = _mainWindow;
        CommandDoBeforeSecondView = new RelayCommand(openSecondView);
    }

    private void openSecondView(object obj)
    {
        Console.WriteLine("DO SOME CODE");
        mainWindow.SelectedViewModel = new SecondPageViewModel();

    }
}

Solution

  • My working Solution:

    MainWindowViewModel.cs

    private void openStartView(object obj)
    {
        SelectedViewModel = new StartPageViewModel(this);
    }
    

    StartPageViewModel.cs

    class StartPageViewModel : BaseViewModel
    {
        private MainWindowViewModel mainWindow;
        public ICommand CommandDoBeforeSecondView { get; set; }
    
        public StartPageViewModel(MainWindowViewModel _mainWindow)
        {
            mainWindow = _mainWindow;
            CommandDoBeforeSecondView = new RelayCommand(openSecondView);
        }
    
        private void openSecondView(object obj)
        {
            Console.WriteLine("DO SOME CODE");
            mainWindow.SelectedViewModel = new SecondPageViewModel();
    
        }
    }
    

    Thx to Peter Duniho he answered it the comments above:

    you should just pass this when creating StartPageViewModel in the MainWindowViewModel, then the StartPageViewModel will have the reference (you'll have to add the parameter to the constructor of course)