以编程方式从内容ViewModel更改ContentControl内容

时间:2017-05-27 23:21:48

标签: c# wpf mvvm contentcontrol

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

    }
}

1 个答案:

答案 0 :(得分:0)

我的工作解决方案:

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致谢,他回答了上述评论:

  

你应该在MainWindowViewModel中创建StartPageViewModel时传递它,然后StartPageViewModel将有引用(你当然必须将参数添加到构造函数中)