WPF 三态图像按钮 [英] WPF TriState Image Button

查看:27
本文介绍了WPF 三态图像按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有创建三态图像按钮的任何指示?

Does anyone have any pointers for creating a tristate image button?

我有以下内容,但我真正想做的是拥有一个具有多个 ImageSource 属性的控件,例如 <Controls.TristateButton Image="" HoverImage="" PressedImage=""/>

I have the following but what I really want to do is have a control with multiple ImageSource properties like <Controls.TristateButton Image="" HoverImage="" PressedImage="" />

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <StackPanel Orientation="Horizontal" >
                    <Image Name="PART_Image" Source="path to normal image" />
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Source" Value="path to mouse over image" TargetName="PART_Image"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Source" Value="path to pressed image" TargetName="PART_Image"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

推荐答案

这个 StackOverflow 问题的公认答案展示了一个简单的方法来做到这一点:WPF - 如何使用模板创建图像按钮

The accepted answer to this StackOverflow question shows an easy way to do this: WPF - How to create image button with template

您可以在 IsEnabled 和 IsPressed 属性上创建属性触发器,并根据需要显示或隐藏图像.

You create property triggers on the IsEnabled and IsPressed properties and show or hide the images as needed.

正如 Avanka 在他的回答中指出的那样,您需要创建依赖属性来设置图像的路径.

As Avanka noted in his answer, you'll need to create dependency properties to set the paths to the images.

这篇关于WPF 三态图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆