从code背后WPF更改按钮图像

问题描述:

我想检查哪些图像应用到该按钮,并改变它在code的后面。

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

您可以绑定来源属性按钮的内容

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>

您现在可以设置code中的内容到任何其他的ImageSource:

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

btnFlashAlert.Content = new BitmapImage(new Uri(...));