If you don't want three different view models, you could add a property to the main view model that decides which view to display:
public class MainViewModel : INotifyPropertyChanged
{
public string SharedSampleProperty { get; set; } = "Some value that is shared across all views...";
private View _currentView = View.First;
public View CurrentView
{
get => _currentView;
set { _currentView = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public enum View
{
First,
Second,
Third
}
In the view, you could then use a ContentControl
and a Style
that sets the ContentTemplate
based on the value of this property:
<ContentControl Content="{Binding}">
<ContentControl.Resources>
<DataTemplate x:Key="{x:Static local:View.First}">
<TextBlock>1...</TextBlock>
</DataTemplate>
<DataTemplate x:Key="{x:Static local:View.Second}">
<TextBlock>2...</TextBlock>
</DataTemplate>
<DataTemplate x:Key="{x:Static local:View.Third}">
<TextBlock>3...</TextBlock>
</DataTemplate>
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding CurrentView}" Value="First">
<Setter Property="ContentTemplate" Value="{StaticResource {x:Static local:View.First}}" />
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}" Value="Second">
<Setter Property="ContentTemplate" Value="{StaticResource {x:Static local:View.Second}}" />
</DataTrigger>
<DataTrigger Binding="{Binding CurrentView}" Value="Third">
<Setter Property="ContentTemplate" Value="{StaticResource {x:Static local:View.Third}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
You may obviously replace the TextBlock
elements with UserControls
in the DataTemplates
.