我成功登录后有一个登录视图,它应该打开菜单视图,因为它有不同的选项卡。我也希望选项卡在菜单视图本身内打开,一旦打开另一个视图就应该关闭视图。 我已提到以下链接: Changing the View for a ViewModel 和 switching views in MVVM wpf。 我做过类似的事情:
MainWindow.xaml
public class MainWindowViewModel : ViewModelBase
{
private ViewModelBase _currentViewModel;
readonly static LoginViewModel _loginViewModel = new LoginViewModel();
readonly static MenuViewModel _menuViewModel = new MenuViewModel();
readonly static UserModuleMapViewModel _usermodulemapViewModel = new UserModuleMapViewModel();
public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
OnPropertyChanged("CurrentViewModel");
}
}
public ICommand FirstViewCommand { get; private set; }
public ICommand SecondViewCommand { get; private set; }
public MainWindowViewModel()
{
CurrentViewModel = MainWindowViewModel._menuViewModel;
FirstViewCommand = new RelayCommand(() => ExecuteFirstViewCommand());
SecondViewCommand = new RelayCommand(() => ExecuteSecondViewCommand());
//ViewModels = new ObservableCollection<ViewModelBase>()
// {
// new LoginViewModel(),
// new MenuViewModel()
// //new ViewModel3()
// };
//ViewModelsView = CollectionViewSource.GetDefaultView(ViewModels);
}
public void ExecuteFirstViewCommand()
{
CurrentViewModel = MainWindowViewModel._usermodulemapViewModel;
}
private void ExecuteSecondViewCommand()
{
CurrentViewModel = MainWindowViewModel._menuViewModel;
}
MainWindowViewModel.cs
['1', ['2', '2x'], '3', '4', ['5', '5x']]
我的第一个屏幕是登录视图,这是完美的但是在成功登录后菜单视图应该打开。我犯了什么错误?
答案 0 :(得分:0)
这是可能的答案之一。 我试图让它变得尽可能简单,例如避免样式。
XAML代码(我创建了LoginViewModel和MenuViewModel作为UserControls来测试它)
<Window x:Class="JGC_ngCMS_Win.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:JGC_ngCMS_Win.View"
xmlns:VM="clr-namespace:JGC_ngCMS_Win.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ContentPresenter Content="{Binding CurrentViewModel}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type VM:LoginViewModel}">
<local:LoginView/>
</DataTemplate>
<DataTemplate DataType="{x:Type VM:MenuViewModel}">
<local:MenuView />
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</Window>
以下是ModelView类的最小可测试代码
class ViewModelBase:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
class MainWindowViewModel:ViewModelBase
{
readonly LoginViewModel _loginViewModel = new LoginViewModel();
readonly MenuViewModel _menuViewModel = new MenuViewModel();
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
OnPropertyChanged("CurrentViewModel");
}
}
//Just for test
public void switchView()
{
if (CurrentViewModel == _loginViewModel) { CurrentViewModel = _menuViewModel; }
else { CurrentViewModel = _loginViewModel; }
}
}
class LoginViewModel:ViewModelBase
{
}
class MenuViewModel:ViewModelBase
{
}
然后你只需要指定DataContext:
ViewModel.MainWindowViewModel mainVM = new ViewModel.MainWindowViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = mainVM;
}
备注:我为DataContext使用了一个显式实例,但如果需要,可以使用静态属性。