更改按钮背景未启用时

更改按钮背景未启用时

本文介绍了更改按钮背景未启用时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要改变我的按钮的背景(如的SolidColorBrush例如)仅在未启用( IsEnabled ==假)。照片我该怎么办?

我是否修改使用XAML或者我可以以编程方式做这项工作的一个按钮风格?什么是正确的XAML code,当未启用,只改变背景?

我尝试以下XAML code,但它没有任何效果:

 <按钮和GT;
< Button.Style>
    <风格的TargetType =按钮>
        < Style.Triggers>
            <触发属性=IsEnabledVALUE =FALSE>
                < setter属性=背景值=红>< /二传手>
            < /触发>
        < /Style.Triggers>
    < /样式和GT;
< /Button.Style>
< /按钮>


解决方案

您可以通过编辑模板更改背景。你会发现按钮的。

在触发 IsEnabled 您可以简单地添加这样的:

 < setter属性=背景VALUE ={StaticResource的DisabledBackgroundBrush}/>

修改
试试这个话;

 < Window.Resources>
    <风格的TargetType ={X:类型按钮}>
        < setter属性=模板>
            < Setter.Value>
                <的ControlTemplate的TargetType =按钮>
                    < BORDER X:NAME =叠加CornerRadius =2>
                        <内容presenter />
                    < /边框>
                    < ControlTemplate.Triggers>
                        <触发属性=IsEnabledVALUE =false的>
                            <二传手的TargetName =叠加属性=背景VALUE =红/>
                        < /触发>
                    < /ControlTemplate.Triggers>
                < /控件模板>
            < /Setter.Value>
        < /二传手>
    < /样式和GT;
< /Window.Resources>
<&StackPanel的GT;
    <按钮内容=按钮IsEnabled =FALSE/>
< / StackPanel的>

只要改变它来满足您的需求。

I need to change my Button background (as SolidColorBrush for example) only when it is not enabled (IsEnabled == false).
How can I do?

Have I to modify the Button Style using the XAML or can I do this work programmatically? What is the correct XAML code, to change only the Background when it is not enabled?

I tried the following XAML code but it has no effect:

<Button>
<Button.Style>
    <Style TargetType="Button">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="Red"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>
</Button>
解决方案

You can change the background by editing the template. You'll find the default template for Button here.

In the trigger for IsEnabled you can simply add something like this:

<Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>

EDIT:Try this instead then;

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="Overlay" CornerRadius="2">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Overlay" Property="Background" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<StackPanel>
    <Button Content="Button" IsEnabled="False"/>
</StackPanel>

Just change it to suit your needs.

这篇关于更改按钮背景未启用时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:39