选中切换按钮时更改切换按钮的背景颜色 [英] Change the background color of a toggle button when the toggle button is checked

查看:36
本文介绍了选中切换按钮时更改切换按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在选中切换按钮时更改切换按钮的背景颜色,反之亦然.

I want to change the background color of a toggle button when the toggle button is checked and vice versa.

我怎样才能做到这一点?

How can I achieve that?

推荐答案

<ToggleButton Content="toggle">
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" 
                                Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center"                  
                                              VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter> 
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

与 Klaus 的几乎相同,但使用TemplateBinding"而不是TargetName".通过 TemplateBinding,ControlTemplate 使用 ToggleButtons DefaultStyle 中的 BorderBrush 和 Background.因此 Trigger 可以设置 ToggleButtons 背景和边框,这也将显示.

Nearly the same as Klaus ones, but using "TemplateBinding" instead of "TargetName". With the TemplateBinding, the ControlTemplate use the BorderBrush and Background from the ToggleButtons DefaultStyle. So the Trigger can set the ToggleButtons Background and with the Border this one will also be shown.

这篇关于选中切换按钮时更改切换按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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