简体   繁体   English

设置按钮的背景图像

[英]Setting a background image for a button

Really stupid question but I have set a background image for my button via XAML. 确实是愚蠢的问题,但是我已经通过XAML为按钮设置了背景图片。 But when I run the application and hover over it changes to default looking skin. 但是,当我运行该应用程序并将其悬停时,它将更改为默认外观。

I can't work out how to do a Mouse over? 我无法解决如何将鼠标悬停在上方吗?

<Button x:Name="btnPlay" Content="Play" Canvas.Left="93" Canvas.Top="93" Width="183" 
            Click="button_Click" Height="84" FontFamily="Showcard Gothic" Cursor="Hand" BorderBrush="{x:Null}">
    <Button.Background>
        <ImageBrush ImageSource="Images/BlueButton.png"/>
    </Button.Background>
</Button>

Here is a modification of this solution . 这是此解决方案的修改。 Consider using Background="{TemplateBinding Background}" for the Border inside the Template , so it update the Background of the Border when Button 's Background changed. 考虑使用Background="{TemplateBinding Background}"BorderTemplate ,所以更新的Background中的Border时, Button的背景变化。

<Button x:Name="btnPlay" Content="Play" Canvas.Left="93" Canvas.Top="93" Width="183" 
        Click="button_Click"   Height="84" FontFamily="Showcard Gothic" Cursor="Hand" 
        BorderBrush="{x:Null}">
    <Button.Background>
        <ImageBrush ImageSource="/WpfApplication1;component/5.png" />
    </Button.Background>
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Border Name="border" Background="{TemplateBinding Background}"> <!--Consider this-->
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <ImageBrush ImageSource="/WpfApplication1;component/5.png" />
                        </Setter.Value>
                    </Setter>
                    <Setter Property="BorderBrush" Value="Black"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM