2

I would like to check which image is applied to the button and change it in code behind.

 <Button x:Name="btnFlashAlert" Content="Button" Canvas.Left="87" Canvas.Top="258" Background="{x:Null}" Margin="136,244,409,215" BorderBrush="{x:Null}" BorderThickness="0" Cursor="Hand" Click="btnFlashAlert_Click">
        <Button.Template>
            <ControlTemplate>
                <Image Source="Main/Images/FlashButton.png" Name="image"/>
            </ControlTemplate>
        </Button.Template>
    </Button>

My goal is to make the button flash with different color button images not colors when certain values are met to inform the user they have certain types of messages waiting

Tsukasa
  • 6,342
  • 16
  • 64
  • 96
  • [This](http://stackoverflow.com/questions/774375/using-a-resource-image-in-code-behind) perhaps? –  Dec 03 '13 at 20:53
  • Setting the background doesn't work, i'm assuming because of the control template and the actual background is x:null. – Tsukasa Dec 03 '13 at 20:55

1 Answers1

3

You may bind the Source property to the Button's Content:

<Button x:Name="btnFlashAlert">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Image Source="{TemplateBinding Content}"/>
        </ControlTemplate>
    </Button.Template>
    <Button.Content>
        <BitmapImage UriSource="Main/Images/FlashButton.png"/>
    </Button.Content>
</Button>

You may now set the Content to any other ImageSource in code:

btnFlashAlert.Content = new BitmapImage(new Uri(...));
Clemens
  • 123,504
  • 12
  • 155
  • 268