Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>CurrentView should be a property om your MainWindowViewModel or whatever class that is your Datacontext on Window.xaml. For each view you define a data template. The data template can contain the view or point to a user control. Assign the CurrentView to a view model to switch view. Here is some code:</p> <p><strong>MainWindowViewModel</strong></p> <pre><code>public class MainWindowViewModel : ViewModel { object currentView; public MainWindowViewModel() { CurrentView = new OptionalView(); SwitchViewCommand = new RelayCommand(SwitchView); } public object CurrentView { get { return this.currentView; } set { this.currentView = value; NotifyPropertyChanged("CurrentView"); } } public RelayCommand SwitchViewCommand { get; set; } void SwitchView() { if (CurrentView is OptionalView) CurrentView = new SettingsView(); else CurrentView = new OptionalView(); } } public class OptionalView { } public partial class SettingsView { } </code></pre> <p><strong>MainWindow</strong></p> <pre><code>&lt;Window x:Class="WpfLab.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:views="clr-namespace:WpfLab.Views" xmlns:vm="clr-namespace:WpfLab.ViewModels" Title="MainWindow" Width="525" Height="350"&gt; &lt;Window.Resources&gt; &lt;DataTemplate DataType="{x:Type vm:OptionalView}"&gt; &lt;Border Background="Red" /&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type vm:SettingsView}"&gt; &lt;views:SettingsView /&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;ContentControl Content="{Binding CurrentView}" /&gt; &lt;Button Grid.Row="1" Command="{Binding SwitchViewCommand}" Content="SwitchView" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p> <strong>Settings View</strong></p> <pre><code>&lt;UserControl x:Class="WpfLab.Views.SettingsView" 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" d:DesignHeight="300" d:DesignWidth="300" mc:Ignorable="d"&gt; &lt;Border Background="Green" /&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload