0

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>

1
  • 4
    There should be a single Style with DataTriggers on a State property. The different property Setters would go into DataTriggers on different State values. Commented Jan 22, 2020 at 14:53

1 Answer 1

3

Use a single Style with DataTriggers that binds to your DataContext or source property:

<Window.Resources>
    <Style x:Key="ButtonStyle" TargetType="Button">
        <Style.Triggers>
            <DataTrigger Binding="{Binding}" Value="StateA">
                <Setter Property="TextBlock.Foreground" Value="Blue" />
            </DataTrigger>
            <DataTrigger Binding="{Binding}" Value="StateB">
                <Setter Property="TextBlock.Foreground" Value="Red" />
            </DataTrigger>
            <DataTrigger Binding="{Binding}" Value="StateC">
                <Setter Property="TextBlock.Foreground" Value="Green" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <Button Name="foo" Content="{Binding}" HorizontalAlignment="Center" 
            VerticalAlignment="Center" Width="75" 
            Click="Button_Click" Style="{StaticResource ButtonStyle}" />
</Grid>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.