在MVVM wpf中切换视图
我知道我的问题是一个普遍的问题,但我发现的每一个解决方案都不是我真正需要的解决方案。 这是我的问题:我想能够在mainWindow中的不同usercontrol之间切换。 我找到的所有解决方案都包含在主窗口中有一个菜单,每个按钮都带有相应的userControl,如下所示:https://rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2/
但是我想要的更像是:在开始时,主窗口中有UserControl1。 在userControl1中,会有1个按钮用新的userControl更改mainWindow的内容(例如userControl2)
mainWindow的xaml
<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:
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();
}
}
我的第一页viewModel:
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”,因为它绑定到了mainWindow的控件
我的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));
}
}
}
非常感谢您的帮助 !
你可以例如注入MenuView
或MenuViewModel
以引用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();
}
}
链接地址: http://www.djcxy.com/p/56189.html
上一篇: switching views in MVVM wpf
下一篇: WPF swtich views using MVVM pattern without Navigation view