以编程方式更改 Content ViewModel 中的 ContentControl 内容

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

    }
 }

问题: 如何使用 StartPage 中的第二个按钮更改 MainWindow ContentControl 中的内容?我想执行一些代码,然后更改内容。

关于第一条评论,我想我必须在我的 StartPageViewModel 中添加对 MainWindowViewModel 的引用,我该怎么做?

编辑

我的工作解决方案:

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();

    }
}

我的工作解决方案:

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();

    }
}

感谢 Peter Duniho,他回答了上面的评论:

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)