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.).
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.
I've tried few alternatives to define the bt the button style. None works (see below).
What is the proper way to define binding that will pick a style from multiple predefined styles of a control at run time ?
<Button Content="Button" Style="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click" />
<Button Content="Button" Style="{DynamicResource {Binding}}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click" />
The Style Section:
<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">
<Window.Resources>
<Style x:Key="StateA" >
<Setter Property="TextBlock.Background" Value="blue" />
</Style>
<Style x:Key="StateB" >
<Setter Property="TextBlock.Background" Value="yellow" />
</Style>
<Style x:Key="StateC" >
<Setter Property="TextBlock.Background" Value="green" />
</Style>
</Window.Resources>
<Grid>
<Button Name="foo" Content="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click" Style="{DynamicResource {Binding}}" />
</Grid>
</Window>