2017-05-27 78 views
-1

MainWindow.xaml更改ContentControl中內容編程

<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 

    } 
} 

問題: 如何更改使用起始頁的第二個按鈕從主窗口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(); 

    } 
} 
+0

你的問題太含糊,使得它也太寬泛。基於你分享的一小部分代碼,你似乎已經有了一個用於你的視圖模型的模板。你明白,將SelectedViewModel設置爲不同的值將改變所使用的視圖模型對象。那麼,你的問題是什麼?只需將該屬性設置爲任何你想要的。只要給你的'StartPageViewModel'作爲對'MainViewModel'對象的引用,或者將'MainViewModel'對象訂閱到'StartPageViewModel'上的一個合適的事件,你可以通過那個引用或事件來改變屬性值。 –

+0

以上依然不是[mcve]。你有不需要在那裏的代碼('BoolToVisConverter'資源),並且你錯過了第二頁代碼(不用介意'RelayCommand',它不是內置的)。根據你發佈的內容,你應該在'MainWindowViewModel'中創建'StartPageViewModel'時傳遞'this',然後'StartPageViewModel'將有引用(當然你必須將該參數添加到構造函數中)。另一種方法是,如果您希望能夠在XAML中聲明所有內容,則只需添加一個可以設置爲正確值的屬性即可。 –

+0

@PeterDuniho我將代碼縮短爲相關代碼,但忘記刪除'BoolToVisConverter'。第二頁代碼只有「第二頁」的標籤。 – Laire

回答

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

    } 
} 

THX彼得Duniho他回答了上述評論:

在創建StartPageViewModel時,你應該通過這MainWindowViewModel,那麼StartPageViewModel將有引用(你將不得不將該參數添加到構造函數當然)