Im currently trying to learn WPF and the MVVM pattern, but am confused when it comes to changing the main windows view from inside a User Control.
What I currently have
I have a single window application which when started displays a login view which is a UserControl and is set as a ContentControl in my main window.
ApplicationView.xaml
<Window x:Class="MyApplication.Views.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ignore="http://www.galasoft.ch/ignore"
xmlns:local="clr-namespace:MyApplication.Views"
xmlns:viewModels="clr-namespace:MyApplication.ViewModels"
mc:Ignorable="d ignore"
DataContext="{Binding Application, Source={StaticResource Locator}}">
<Window.Resources>
<DataTemplate DataType="{x:Type viewModels:LoginViewModel }">
<local:LoginView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:MainViewViewModel }">
<local:MainView />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding CurrentPageViewModel}"></ContentControl>
</Grid>
</Window>
ApplicationViewModel.cs
namespace MyApplication.ViewModels
{
public class ApplicationViewModel : ViewModelBase
{
#region Fields
private ICommand _changePageCommand;
private IPageViewModel _currentPageViewModel;
private List<IPageViewModel> _pageViewModels;
#endregion
public ApplicationViewModel()
{
//Add available pages
PageViewModels.Add(new LoginViewModel());
PageViewModels.Add(new MainViewViewModel());
//Set starting page
CurrentPageViewModel = PageViewModels[0];
}
#region Properties / Commands
public ICommand ChangePageCommand
{
get
{
if (_changePageCommand == null)
{
_changePageCommand = new RelayCommand<object>(
p => ChangeViewModel((IPageViewModel)p),
p => p is IPageViewModel);
}
return _changePageCommand;
}
}
public List<IPageViewModel> PageViewModels
{
get
{
if (_pageViewModels == null)
_pageViewModels = new List<IPageViewModel>();
return _pageViewModels;
}
}
public IPageViewModel CurrentPageViewModel
{
get
{
return _currentPageViewModel;
}
set
{
if (_currentPageViewModel != value)
{
_currentPageViewModel = value;
this.VerifyPropertyName("CurrentPageViewModel");
this.RaisePropertyChanged();
}
}
}
#endregion
#region Methods
private void ChangeViewModel(IPageViewModel viewModel)
{
if (!PageViewModels.Contains(viewModel))
PageViewModels.Add(viewModel);
CurrentPageViewModel = PageViewModels
.FirstOrDefault(vm => vm == viewModel);
}
#endregion
}
}
What I want to achieve
I want to change the applications window view from the LoginView to the MainView once a login button inside the login UserControl is clicked. I'm struggling to get my head around how this would be possible and how to achieve this. Any help would be greatly appreciated, Thanks.