本文介绍了如何制作 2 状态图像按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很惊讶我没有在这里找到这个简单问题的答案.我想用图像内容创建一个简单的按钮.按下时,图像应更改为另一个图像,释放按钮后更改回原始图像.最简单的方法是什么?

I'm actually surprised that I didn't find the answer to this simple question here. I want to create a simple button with Image content. When pressed the image should change to another one and after the button is released change back to the original image. What is the easiest way to do this?

这不是切换按钮!

推荐答案

您可以使用 ControlTemplate 来指定按钮的视觉结构.

You can achieve this using a ControlTemplate to specify the visual structure of your button.

下面的 XAML 显示了如何将图像添加到您的按钮(保持标准的 chrome 边框,但如果您不想要它,您可以将其删除),并在 isPressed 事件上添加一个触发器来设置您的其他图像.如果您想使用 chrome 按钮边框,您还需要包含命名空间 xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna".>

The below XAML shows how to add an image to your button (maintaining the standard chrome border though you can remove this if you don't want it), plus adding a trigger on the isPressed event to set your other image. You'll need to include the namespace xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna" as well if you want to use the chrome button border.

    <Button>
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true" ThemeColor="NormalColor">
                <Image x:Name="buttonImage" Source="defaultImage"/>
            </Microsoft_Windows_Themes:ButtonChrome>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="true">
                    <Setter Property="Source" TargetName="buttonImage" Value="onPressedImage"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

这篇关于如何制作 2 状态图像按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:18