为什么禁用按钮来删除它的背景色? [英] Why does disabling a button remove its background color?

查看:113
本文介绍了为什么禁用按钮来删除它的背景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,其背景是由一个LinearGradientBrush 指定。我需要禁用此按钮。所以,我只设置了 isEnabled 属性。但是,这消除了背景也和因为我的按钮没有边框,我已经离开只是在按钮上的文本。下面是我的按钮的XAML代码:

I have a button whose background is specified by a LinearGradientBrush. I need to disable this button. So, I just set its isEnabled property to "false". But, that removes the background too and since my button doesn't have a border, what I have left is just the text on the button. Here's the xaml code for my button:

<Button x:Name="create" Content="Create a new one?" Margin="12, 0, 12, 0" ClickMode="Release" Click="create_click" MinWidth="330" MinHeight="50" BorderThickness="0" Height="110" Foreground="Black">
    <Button.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
             <GradientStop Color="#FFFA2FC5" Offset="0.279" />
             <GradientStop Color="#FF5904A0" Offset="1" />
        </LinearGradientBrush>
    </Button.Background>
</Button>



而当我设 isEnabled 真实再次,背景回来,一切都和好如初。结果

And when I set isEnabled to "true" again, the background comes back and everything is good again.

请问这是什么应该发生?结果
我应该怎么做,如果我只是想使按钮非功能性的同时不改变其外观?

Is this what is supposed to happen?
What should I do if I just want to make the button non-functional for a while without changing its appearance?

推荐答案

您需要编辑相应的VisualState改变禁用按钮的外观。要做到这一点,最简单的方法是使用Expression Blend中

You need to edit the appropriate VisualState to change the look of the disabled button. The easiest way to do this is to use Expression Blend.

通过形式打开,右键单击该按钮并选择编辑模板 - <编辑副本。这会将XAML的一大块在页面资源部分定义了控制VisualStates,以及实例的风格结合新定义的风格。 XAML中看起来像这样...

With the form open, Right click on the button and select Edit Template -< Edit a copy. This will put a chunk of XAML in the page resources section which defines the VisualStates of the control, as well as binding the style of the instance to the newly defined style. The Xaml will look like this...

 <Style x:Key="ButtonStyle1" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
    <Setter Property="Padding" Value="10,3,10,5"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}">
                        <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

和你关心的是位名为禁用的元素的VisualState。已经有定义的,所以你只需要编辑透明的背景。

And the bit you are concerned with is the VisualState element with the name "Disabled". There is already a background of "Transparent" defined, so you just need to edit that.

如果您手动这样做,你需要设置样式={StaticResource的ButtonStyle1}你的按钮使其生效。

If you are doing this manually, you will need to set Style="{StaticResource ButtonStyle1}" on your button for this to take effect.

这篇关于为什么禁用按钮来删除它的背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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