title: Change WPF control style at runtime via Binding
body: <p>For a game that I'm developing, the code need to change (at runtime) the style of a button. Basically, the button represent a cell in (chess-like) game, and the attribute of the cell (background, foreground, or image) represent the state of the cell (captured, damaged, enemy, etc.).</p>
  
  <p>The application calculate the style name of the button, and set the DataContext of the button to 'StateA', 'StateB', 'StateC' or 'StateD', depending on the state of the cell.</p>
  
  <p>I've tried few alternatives to define the bt the button style. None works (see below).</p>
  
  <p>What is the proper way to define binding that will pick a style from multiple predefined styles of a control at run time ?</p>
  
  <pre><code>        &lt;Button Content="Button"  Style="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click" /&gt;
  
          &lt;Button Content="Button" Style="{DynamicResource {Binding}}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click" /&gt;
  </code></pre>
  
  <p>The Style Section:</p>
  
  <pre><code>&lt;Window x:Class="WpfApp2.MainWindow"
          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"
          xmlns:local="clr-namespace:WpfApp2"
          mc:Ignorable="d"
          Title="MainWindow" Height="450" Width="800"&gt;
  
      &lt;Window.Resources&gt;
          &lt;Style x:Key="StateA"  &gt;
              &lt;Setter Property="TextBlock.Background" Value="blue" /&gt;
          &lt;/Style&gt;
          &lt;Style x:Key="StateB" &gt;
              &lt;Setter Property="TextBlock.Background" Value="yellow" /&gt;
          &lt;/Style&gt;
          &lt;Style x:Key="StateC"  &gt;
              &lt;Setter Property="TextBlock.Background" Value="green" /&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;Button Name="foo" Content="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click" Style="{DynamicResource {Binding}}" /&gt;
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  
  </code></pre>