title: WPF Dynamic UserControl Style At Runtime
body: <p>I have a user control with a togglebutton and a couple of textblocks/labels.  I have two styles in my xaml for the toggle button style.  I add these usercontrols dynamically when my form loads.  I want to be able to apply different styles at runtime in my application.  I can't quite figure out how to do this.  Here is my user control:</p>
  
  <pre><code>&lt;UserControl x:Class="UserControl1"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" Height="122" Width="170" BorderThickness="1"&gt; 
  &lt;UserControl.Resources&gt;
      &lt;Style x:Key="Test2" TargetType="{x:Type ToggleButton}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type ToggleButton}"&gt;
                      &lt;Border HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="border" Background="Magenta"  BorderBrush="#FF000000" BorderThickness="1,1,1,1" SnapsToDevicePixels="True"&gt;
                          &lt;ContentPresenter x:Name="contentPresenter"/&gt;
                      &lt;/Border&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsChecked" Value="true"&gt;
                              &lt;Setter Property="Background" TargetName="border" Value="Gainsboro"/&gt;
  
  
                          &lt;/Trigger&gt;
                      &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
      &lt;Style x:Key="Test3" TargetType="{x:Type ToggleButton}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type ToggleButton}"&gt;
                      &lt;Border HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="border" Background="Beige"  BorderBrush="#FFF30808" BorderThickness="4,4,4,4" SnapsToDevicePixels="True"&gt;
                          &lt;ContentPresenter x:Name="contentPresenter"/&gt;
                      &lt;/Border&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsChecked" Value="true"&gt;
                              &lt;Setter Property="Background" TargetName="border" Value="Gainsboro"/&gt;
  
  
                          &lt;/Trigger&gt;
                      &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
     &lt;/UserControl.Resources&gt;
  
  &lt;Grid Margin="0,0,10,10"&gt;
      &lt;WrapPanel HorizontalAlignment="Left" d:DesignHeight="133" VerticalAlignment="Top" d:DesignWidth="172" Margin="0,0,-11,-10"&gt;
          &lt;ToggleButton x:Name="btnUserButton"  HorizontalAlignment="Left" VerticalAlignment="Top" d:DesignWidth="229" d:DesignHeight="101" ClipToBounds="true" Width="166" Height="100" Click="onButtonClick" Background="#FFA29E9E" Style="{StaticResource Test2}" &gt;
              &lt;StackPanel&gt;
                  &lt;TextBlock Text="Title" d:DesignFontSize="35" d:DesignHeight="40" d:DesignWidth="201" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap"  ClipToBounds="true" FontSize="30"/&gt;
                  &lt;TextBlock Text="Body" d:DesignFontSize="35" d:DesignHeight="40" d:DesignWidth="201" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap" Name="cavityNumber"  ClipToBounds="true" FontSize="30"/&gt;
              &lt;/StackPanel&gt;
          &lt;/ToggleButton&gt;
          &lt;Label x:Name="lblYield" Content="Label"  VerticalAlignment="Top" d:DesignWidth="106" HorizontalContentAlignment="Center" Foreground="Black" d:DesignFontSize="20" d:DesignHeight="38"  ClipToBounds="true" Width="81" /&gt;
          &lt;Label x:Name="lblScrap" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Center" Foreground="Red"  ClipToBounds="true" Width="76"/&gt;
      &lt;/WrapPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>I have added a 'style' property to my control, but I don't know if I need it or what to do with it.</p>
  
  <pre><code>    Property thestyle() As Style
      Get
          Return mystyle
      End Get
      Set(value As Style)
          mystyle = value
          btnUserButton.Style = mystyle
      End Set
  End Property
  </code></pre>
  
  <p>What I would like to do is somehow access styles 'Test2' and 'Test3' at runtime.  These styles will be based off of a user selection.  </p>
  
  <pre><code>MyUserControl.Style = DirectCast(FindResource("Test2"), Style)
  </code></pre>
  
  <p>That, of course, does not work.  I read on here about application resources, but I am not sure how to incorporate that into my usercontrol.  Anyway, thanks for any help you can give me on this...it's quite confusing being I am somewhat new to WPF.  Thanks, again.</p>