如何制作一个双状态图像按钮



我真的很惊讶我没有在这里找到这个简单问题的答案。我想创建一个简单的按钮与图像内容。当按下图像时,图像应该变成另一个,按钮释放后,图像会变回原来的图像。最简单的方法是什么?

编辑:这不是切换按钮!

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

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

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

最新更新