title: How to make WPF resource styles switchable at runtime?
body: <p>I've got style resources in WPF working so that in my App.xaml I can define whether the customer or administrator layout is used. This works nicely by getting all the style out of the main XAML in a HTML/CSS-like fashion. </p>
  
  <p><strong>Now how do I make this dynamic</strong> so that I can click on a button in my application and switch the layout between customer and administrator at runtime?</p>
  
  <h1>Addendum:</h1>
  
  <p>Thanks Meeh for the links, so I made this and when I debug, it steps through all the lines but <strong>still doesn't change the layout</strong>, what else do I need to do so that the layout changes at runtime?</p>
  
  <pre><code>private void Button_Switch_Layout_Click(object sender, RoutedEventArgs e)
  {
      string layoutIdCode = "administrator";
  
      switch (layoutIdCode)
      {
          case "administrator":
              langDictPath = "Resources/AdministratorLayout.xaml";
              break;
  
          case "customer":
              langDictPath = "Resources/CustomerLayout.xaml";
              break;
      }
  
      Uri langDictUri = new Uri(langDictPath, UriKind.Relative);
      ResourceDictionary langDict = Application.LoadComponent(langDictUri) as ResourceDictionary;
      Application.Current.Resources.MergedDictionaries.Clear();
      Application.Current.Resources.MergedDictionaries.Add(langDict);
  }
  </code></pre>
  
  <h2>&nbsp;</h2>
  
  <h2>Original Code:</h2>
  
  <hr/>
  
  <p><strong>App.xaml:</strong></p>
  
  <pre><code>&lt;Application x:Class="TestMvvm8837.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      StartupUri="Window1.xaml"&gt;
      &lt;Application.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="Resources/CustomerLayout.xaml"/&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p><strong>Window1.xaml:</strong></p>
  
  <pre><code>&lt;Window x:Class="TestMvvm8837.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Style="{StaticResource MainWindow}"
      Title="Resource Test" &gt;
      &lt;DockPanel&gt;
  
          &lt;Grid Style="{StaticResource Form}"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="Auto" /&gt;
                  &lt;RowDefinition Height="Auto" /&gt;
                  &lt;RowDefinition Height="Auto" /&gt;
                  &lt;RowDefinition Height="Auto" /&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="Auto" /&gt;
                  &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
  
              &lt;TextBlock Style="{StaticResource FormLabel}" Grid.Column="0" Grid.Row="0" Text="First Name" /&gt;
              &lt;TextBlock Style="{StaticResource FormLabel}" Grid.Column="0" Grid.Row="1" Text="Last Name" /&gt;
              &lt;TextBlock Style="{StaticResource FormLabel}" Grid.Column="0" Grid.Row="2" Text="E-Mail" /&gt;
  
              &lt;TextBox Grid.Column="1" Grid.Row="0"/&gt;
              &lt;TextBox Grid.Column="1" Grid.Row="1"/&gt;
              &lt;TextBox Grid.Column="1" Grid.Row="2"/&gt;
  
              &lt;Button Style="{StaticResource FormSaveButton}" Grid.Column="1" Grid.Row="3" Content="Save"/&gt;
  
          &lt;/Grid&gt;
  
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>AdministratorLayout.xaml:</strong></p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
      &lt;Style x:Key="MainWindow" TargetType="{x:Type Window}"&gt;
          &lt;Setter Property="Width" Value="800"/&gt;
          &lt;Setter Property="Height" Value="600"/&gt;
      &lt;/Style&gt;
  
      &lt;Style x:Key="Form" TargetType="{x:Type Grid}"&gt;
          &lt;Setter Property="Margin" Value="20"/&gt;
      &lt;/Style&gt;
  
      &lt;Style x:Key="FormLabel" TargetType="{x:Type TextBlock}"&gt;
          &lt;Setter Property="Margin" Value="10"/&gt;
          &lt;Setter Property="FontFamily" Value="Verdana"/&gt;
          &lt;Setter Property="FontSize" Value="14"/&gt;
      &lt;/Style&gt;
  
      &lt;Style TargetType="{x:Type TextBox}"&gt;
          &lt;Setter Property="Margin" Value="10"/&gt;
          &lt;Setter Property="Width" Value="200"/&gt;
          &lt;Setter Property="HorizontalAlignment" Value="Left"/&gt;
          &lt;Setter Property="VerticalAlignment" Value="Top"/&gt;
      &lt;/Style&gt;
  
      &lt;Style x:Key="FormSaveButton" TargetType="{x:Type Button}"&gt;
          &lt;Setter Property="Margin" Value="10"/&gt;
          &lt;Setter Property="FontFamily" Value="Verdana"/&gt;
          &lt;Setter Property="FontSize" Value="14"/&gt;
          &lt;Setter Property="Width" Value="100"/&gt;
          &lt;Setter Property="HorizontalAlignment" Value="Left"/&gt;
          &lt;Setter Property="VerticalAlignment" Value="Top"/&gt;
          &lt;Setter Property="Cursor" Value="Hand"/&gt;
      &lt;/Style&gt;
  
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p><strong>CustomerLayout.xaml:</strong></p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
      &lt;Style x:Key="MainWindow" TargetType="{x:Type Window}"&gt;
          &lt;Setter Property="Width" Value="600"/&gt;
          &lt;Setter Property="Height" Value="480"/&gt;
          &lt;Setter Property="Background"&gt;
              &lt;Setter.Value&gt;
                  &lt;LinearGradientBrush EndPoint="1.156,1.133" StartPoint="-0.033,0.019"&gt;
                      &lt;GradientStop Color="#FFFFFFFF" Offset="0"/&gt;
                      &lt;GradientStop Color="#FF4FADD3" Offset="1"/&gt;
                  &lt;/LinearGradientBrush&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
      &lt;Style x:Key="Form" TargetType="{x:Type Grid}"&gt;
          &lt;Setter Property="Margin" Value="5"/&gt;
      &lt;/Style&gt;
  
      &lt;Style x:Key="FormLabel" TargetType="{x:Type TextBlock}"&gt;
          &lt;Setter Property="Margin" Value="10"/&gt;
          &lt;Setter Property="FontFamily" Value="Verdana"/&gt;
          &lt;Setter Property="FontSize" Value="10"/&gt;
      &lt;/Style&gt;
  
      &lt;Style TargetType="{x:Type TextBox}"&gt;
          &lt;Setter Property="Margin" Value="10"/&gt;
          &lt;Setter Property="Width" Value="200"/&gt;
          &lt;Setter Property="HorizontalAlignment" Value="Left"/&gt;
          &lt;Setter Property="VerticalAlignment" Value="Top"/&gt;
      &lt;/Style&gt;
  
      &lt;Style x:Key="FormSaveButton" TargetType="{x:Type Button}"&gt;
          &lt;Setter Property="Margin" Value="10"/&gt;
          &lt;Setter Property="FontFamily" Value="Verdana"/&gt;
          &lt;Setter Property="FontSize" Value="10"/&gt;
          &lt;Setter Property="Width" Value="100"/&gt;
          &lt;Setter Property="HorizontalAlignment" Value="Left"/&gt;
          &lt;Setter Property="VerticalAlignment" Value="Top"/&gt;
          &lt;Setter Property="Cursor" Value="Hand"/&gt;
      &lt;/Style&gt;
  
  &lt;/ResourceDictionary&gt;
  </code></pre>