2017-02-14 89 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