Dockson
Dockson

Reputation: 562

Why is this WPF Button background not changing behind an image?

My squares are either SaddleBrown or WhiteSmoke in color, shifting to DarkTurquoise when selected. This works when there is no image on top of the squares. When I have a (PNG) image on top of the square the original SaddleBrown/WhiteSmoke color shows behind it but when the color is supposed to change to DarkTurquoise nothing happens to the background color of the square.

What could be the problem?

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Button x:Name="Square"
                Command="{Binding DataContext.BoardGUI.SquareClickCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                CommandParameter="{Binding}">
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <Grid Background="{TemplateBinding Background}">
                        <Image Source="{Binding Source, Converter={StaticResource NullImageConverter}}"/>
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding SquareColor}" Value="Dark">
                <Setter TargetName="Square" Property="Background" Value="SaddleBrown"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding SquareColor}" Value="White">
                <Setter TargetName="Square" Property="Background" Value="WhiteSmoke"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                <Setter TargetName="Square" Property="Background" Value="DarkTurquoise"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ItemsControl.ItemTemplate>

Upvotes: 2

Views: 368

Answers (1)

plast1k
plast1k

Reputation: 843

As determined in the comments, based on the screenshot and the definition of the triggers, it appears that "SquareColor" is likely getting set or changed after IsSelected is set.

Upvotes: 1

Related Questions