2017-02-14 104 views
0

我知道我的問題是常見的問題,但我發現的每個解決方案都不是我真正需要的解決方案。這是我的問題:我想能夠在mainWindow中的不同usercontrol之間切換。我發現的所有解決方案都包含在主窗口中有一個菜單,每個按鈕都帶有相應的userControl,如下例所示:https://rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2/在MVVM中切換視圖wpf

但是我想要的更像是:在開始時,主窗口中有UserControl1 。在的UserControl1會有1個按鈕誰改變主窗口的一個新的用戶控件的內容(userControl2例如)

enter image description here

的主窗口

<Window x:Class="DataTemplateSO_Learning.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:DataTemplateSO_Learning" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources>  
     <DataTemplate DataType="{x:Type local:EmployeeViewModel}"> 
      <local:EmployeeView/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type local:DepartmentViewModel}"> 
      <local:DepartmentView/> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type local:MenuViewModel}"> 
      <local:MenuView/> 
     </DataTemplate> 
    </Window.Resources> 
    <DockPanel LastChildFill="True"> 
     <ContentControl x:Name="Pages" DockPanel.Dock="Right" Content="{Binding SelectedViewModel}"/> 
    </DockPanel> 
</Window> 

我的主窗口的CS的XAML:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     Pages.Content = new MenuView(); 
     this.DataContext = new NavigationViewModel(); 
    } 
} 

我的第一頁的XAML:

<UserControl x:Class="DataTemplateSO_Learning.MenuView" 
      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:local="clr-namespace:DataTemplateSO_Learning" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <DockPanel LastChildFill="True"> 
     <StackPanel x:Name="navigation" DockPanel.Dock="Left" VerticalAlignment="Center"> 
      <Button Content="Employee" Command="{Binding EmpCommand}"></Button> 
      <Button Content="Department" Command="{Binding DeptCommand}"></Button> 
     </StackPanel> 
    </DockPanel> 
</UserControl> 

我的第一頁查看:

public partial class MenuView : UserControl 
{ 
    public MenuView() 
    { 
     InitializeComponent(); 
     this.DataContext = new MenuViewModel(); 
    } 
} 

我的第一頁的視圖模型:

class MenuViewModel 
{ 
    public ICommand EmpCommand { get; set; } 
    public ICommand DeptCommand { get; set; } 

    public MenuViewModel() 
    { 
     EmpCommand = new BaseCommand(OpenEmp); 
     DeptCommand = new BaseCommand(OpenDept); 
    } 

    private void OpenEmp(object obj) 
    { 
     SelectedViewModel = new EmployeeViewModel(); 
    } 
    private void OpenDept(object obj) 
    { 
     SelectedViewModel = new DepartmentViewModel(); 
    } 
} 

,當然他不知道「SelectedViewModel」,因爲它綁定到的主窗口

控制

my navigationViewModel:

class NavigationViewModel : INotifyPropertyChanged 
{ 
    private object selectedViewModel; 

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

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string propName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 

} 

非常感謝您的幫助!

+1

您需要使用數據模板並將它們與您的嵌入式視圖模型相關聯。然後,您需要創建一個命令,以便在主窗體的視圖模型上觸發該按鈕,以更新「當前」嵌入式視圖模型並調用通知屬性已更改。你的視圖模型在哪裏?請告訴我們的代碼。這聽起來像你需要從頭開始,即找到一個「初學者的WPF和MVVM」教程。我就是這樣開始的。 –

+0

分享您嘗試的代碼,並告訴我們您是否遇到任何特定問題。 – Versatile

+0

@ rory.ap這裏是我的代碼(以上) – Json

回答

2

例如,您可以注入MenuViewMenuViewModel與參考MainViewModel

MainWindow.xaml.cs:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     var viewModel = new NavigationViewModel(); 
     viewModel.SelectedViewModel = new MenuViewModel(viewModel); 
     this.DataContext = viewModel; 
    } 
} 

MenuViewModel.cs:

class MenuViewModel 
{ 
    public ICommand EmpCommand { get; set; } 
    public ICommand DeptCommand { get; set; } 

    private readonly NavigationViewModel _navigationViewModel; 

    public MenuViewModel(NavigationViewModel navigationViewModel) 
    { 
     _navigationViewModel = navigationViewModel; 
     EmpCommand = new BaseCommand(OpenEmp); 
     DeptCommand = new BaseCommand(OpenDept); 
    } 

    private void OpenEmp(object obj) 
    { 
     _navigationViewModel.SelectedViewModel = new EmployeeViewModel(); 
    } 
    private void OpenDept(object obj) 
    { 
     _navigationViewModel.SelectedViewModel = new DepartmentViewModel(); 
    } 
} 

MenuView.xaml.cs:

public partial class MenuView : UserControl 
{ 
    public MenuView() 
    { 
     InitializeComponent(); 
    } 
} 
+0

好的!但它告訴我: 不可使用的成員'MenuViewModel'不能像方法一樣使用。 – Json

+0

非調用成員'MenuViewModel'不能像方法一樣使用 – Json

+0

哦,我錯過了「新」關鍵字。我編輯了我的答案。 – mm8