简体   繁体   English

无法更改Button的背景图片

[英]Can't change Button's background image

I have a problem with button control. 我的按钮控制有问题。 When I hover over it, the button's image is missing. 当我将鼠标悬停在它上面时,按钮的图像丢失了。 However, when mouse leaves it, the image comes back. 但是,当鼠标离开时,图像会返回。 I want to preserve the image when mouse over is true. 当鼠标悬停为真时,我想保留图像。

this is when mouse is over 这是鼠标结束时

在此处输入图片说明

this is normal status 这是正常状态

在此处输入图片说明

and this is my code 这是我的代码

 <Button Name="miliage_btn" Canvas.Left="775" Canvas.Top="57" Width="239" Height="80" Click="Button_Click" >
                    <Button.Background>
                        <ImageBrush ImageSource="D:\남경현\로드오브다이스 시뮬/pack0.jpg">
                        </ImageBrush>
                    </Button.Background>
                </Button>

and i try this code 我尝试这段代码

<Button.Style>
                        <Style TargetType="Button">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <ImageBrush ImageSource="D:\남경현\로드오브다이스 시뮬/1회뽑기.png"/>
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>

Can it be fixed either in xaml or c#? 可以用xaml或c#修复吗?

There are a few things to consider when creating a control style. 创建控件样式时,需要考虑一些事项。

First, when you declare a Trigger, the target property of any Setters in that Trigger must not be set directly on the control (like you did by assigning Button.Background directly). 首先,在声明触发器时,不得直接在控件上设置该触发器中任何设置器的target属性(就像您直接分配Button.Background )。 It must instead also be set by a Setter in the Style. 相反,还必须由“设置员”在“样式”中设置。 The reason is explained in Dependency Property Value Precedence . 原因在依赖属性值优先级中进行了说明

Second, the default ControlTemplate of a Button shows a visual overlay for mouse over, which overlays the Background. 其次,按钮的默认ControlTemplate显示鼠标悬停时的可视覆盖层,该覆盖层覆盖背景。 You should replace that by declaring your own ControlTemplate. 您应该通过声明自己的ControlTemplate来替换它。

Instead of loading images from absolute file paths, you should consider adding the image files to your Visual Studio project (eg in a folder called Images) and set their Build Action to Resource . 与其从绝对文件路径加载图像,不如考虑将图像文件添加到Visual Studio项目中(例如,在名为Images的文件夹中),并将其Build Action设置为Resource

<Button Content="Hello">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <ContentPresenter
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="Images/StandardBackground.png"/>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <ImageBrush ImageSource="Images/MouseOverBackground.png"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

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

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