【问题标题】:Setting a background image for a button为按钮设置背景图像
【发布时间】:2017-05-21 22:01:55
【问题描述】:

真是愚蠢的问题,但我已经通过 XAML 为我的按钮设置了背景图像。但是当我运行应用程序并将鼠标悬停在它上面时,它会变为默认外观。

我不知道怎么做鼠标悬停?

<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>

【问题讨论】:

  • 检查这个答案:stackoverflow.com/questions/12271916/…。下次记得使用一个好的标题,而不是通用的。
  • 是的,这是按钮默认控件模板使用的按钮镶边,它与鼠标悬停时的背景混淆。正如 Baro 所指出的那样,您基本上需要为按钮定义自己的控件模板以摆脱它...

标签: c# wpf xaml mouse controltemplate


【解决方案1】:

这是对this solution 的修改。考虑将Background="{TemplateBinding Background}" 用于Template 内的Border,因此当Button 的背景发生变化时,它会更新BorderBackground

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    相关资源
    最近更新 更多