I have an example that i wrote long ago and it does not require any framework nonsense, based on my experience the WPF MVVM frameworks are useless for the most part and tend to complicate simple things all you need is an ICommand
implementation and a ViewModelBase
which implements INotiftyPropertyChanged
, here is a simple illustration:
XML:
<Window.Resources>
<DataTemplate DataType="{x:Type local:ViewModel1}">
<local:View1/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:ViewModel2}">
<local:View2/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:ViewModel3}">
<local:View3/>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ContentControl Content="{Binding CurrentViewModel}">
</ContentControl>
<StackPanel Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Bottom">
<Button Command="{Binding PrevViewModel}">Previouws View</Button>
<Button Command="{Binding NextViewModel}">Next View</Button>
<Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel1}">Switch To View1</Button>
<Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel2}">Switch To View2</Button>
<Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel3}">Switch To View3</Button>
</StackPanel>
</Grid>
Given the above when ever the CurrentViewModel
property changes, the View will get selected based on the DataTemplate
resources, and the DataContext of the Window is set to the MainViewModel
.
The main ViewModel looks like the following:
public class MainViewModel : ViewModelBase
{
public ObservableCollection<ViewModelBase> ViewModels { get; set; } = new ObservableCollection<ViewModelBase>();
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel {
get { return _currentViewModel; }
set { SetField(ref _currentViewModel, value); }
}
private ICommand _nextViewModel;
public ICommand NextViewModel
{
get
{
return _nextViewModel = _nextViewModel ?? new RelayCommand(p =>
{
CurrentViewModel = ViewModels[ViewModels.IndexOf(CurrentViewModel) +1];
}, p =>
{
return ViewModels.IndexOf(CurrentViewModel) + 1 != ViewModels.Count && CurrentViewModel != null;
});
}
}
public ICommand _prevViewModel;
public ICommand PrevViewModel
{
get
{
return _prevViewModel = _prevViewModel ?? new RelayCommand(p =>
{
CurrentViewModel = ViewModels[ViewModels.IndexOf(CurrentViewModel) - 1];
}, p =>
{
return ViewModels.IndexOf(CurrentViewModel) != 0 && CurrentViewModel!=null;
});
}
}
private ICommand _switchToViewModel;
public ICommand SwitchToViewModel
{
get
{
return _switchToViewModel = _switchToViewModel ?? new RelayCommand(p =>
{
CurrentViewModel = this.ViewModels.FirstOrDefault(vm=>vm.GetType()==p as Type);
}, p =>
{
return this.ViewModels.FirstOrDefault(vm => vm.GetType() != p as Type) != null;
});
}
}
}
The result looks like

<ContentControl mvvm:RegionManager.RegionName="MyRegion"/>
), register your views for navigation (IUnityContainer.RegisterTypeForNavigation
), then do the navigation in the command bound to your button (RegionManager.RequestNavigate
) – Haukinger