【问题标题】:How do I change a button image but still select the whole button?如何更改按钮图像但仍选择整个按钮?
【发布时间】:2013-10-30 15:13:13
【问题描述】:

我正在尝试在 WPF 中制作一个按钮,当鼠标悬停时,它会亮起并在其周围显示蓝色的选择。我管理了前者,但通过更改按钮图像,我显然覆盖了用蓝色选择突出显示按钮的命令。

这就是我所拥有的:

<Button Command="DoSomething" Name="button">
    <Button.Template>
        <ControlTemplate>
            <Image Height="32" Width="32" Stretch="Uniform" Name="buttonImage">
                <Image.Style>
                    <Style TargetType="Image">
                        <Setter Property="Source" Value="/Project;component/Project/Bitmaps/Icon_colour.png" />
                    </Style>
                </Image.Style>
            </Image>
            <ControlTemplate.Triggers>
               <Trigger Property="IsMouseOver" Value="false">
                   <Setter Property="Source" Value="/Project;component/Project/Bitmaps/Icon_grey.png" TargetName="buttonImage"/>
               </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

有没有办法在保持图标点亮效果的同时重新获得选择?

【问题讨论】:

    标签: wpf image button


    【解决方案1】:

    您已经替换了默认的Button ControlTemplate,因此您需要做的就是“替换”原始ControlTemplate 中缺少的部分。您可以在 MSDN 上的 Button Styles and Templates 页面中找到它。


    更新>>>

    或者,您可以简单地将Image 添加到Button.Content 属性中:

    <Button Command="{Binding DoSomething, Mode=OneWay}">
        <Image Source="/Project;component/Project/Bitmaps/Icon_colour.png" />
    </Button>
    

    【讨论】:

    • 抱歉,我对 WPF 的经验不是很丰富,无法立即获取所有代码。我应该将其中的哪一部分复制到哪里?一切?
    • 很好,这对你来说应该是一个很好的练习。从整个ControlTemplate 开始,首先获取它,这样您的Button 看起来就像默认的一样。 然后进行您想要的更改。
    • 哈哈,好吧,我根本不介意练习,我会看看我怎么做的。但是定义按钮样式和东西有点超出了我的想法范围。
    • 什么意思?您已经为 Button 定义了自己的 ControlTemplate...
    • 好吧,我知道如何在Button 中使用ControlTemplate,是的。但是网站上的例子有一个巨大的样式模板(如果你明白我的意思,按钮的实际位置),所以我很难将它整合到我的项目中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多