为什么禁用按钮会移除它的背景颜色?

本文关键字:背景 颜色 按钮 为什么 | 更新日期: 2023-09-27 18:18:30

我有一个按钮,其背景由LinearGradientBrush指定。我需要禁用这个按钮。我把它的isEnabled属性设为"false"。但是,这也删除了背景,因为我的按钮没有边框,我留下的只是按钮上的文本。下面是我的按钮的xaml代码:

<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设置为"true"时,背景又回来了,一切又好了。

这是应该发生的吗?
如果我只是想让按钮暂时不起作用而不改变其外观,我该怎么办?

为什么禁用按钮会移除它的背景颜色?

您需要编辑适当的VisualState来更改禁用按钮的外观。最简单的方法是使用Expression Blend。

打开表单后,右键单击按钮并选择编辑模板-<编辑副本。这将在页面资源部分中放置一块XAML,该部分定义控件的VisualStates,并将实例的样式绑定到新定义的样式。Xaml将看起来像这样…>

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

您关心的位是名称为"Disabled"的VisualState元素。背景已经定义为Transparent,所以你只需要编辑它。

如果你是手动操作,你需要在按钮上设置Style="{StaticResource ButtonStyle1}"以使其生效

更简单的方法是定义一个样式,在IsEnabled上有一个触发器,它会设置你想要的背景。

  <Button >
    <Button.Style>
           <Style   TargetType="{x:Type Button}" >
             <Setter Property="Background" Value=" the Enabled color i want"/>
                   <Style.Triggers>
                         <Trigger Property="IsEnabled" Value="True">
                           <Setter Property="Background" Value=" the Disabled color i want"/>
                         </Trigger>
                    </Style.Triggers>
            </Style>
      </Button.Style>
    </Button>

或者在资源中定义你的样式,如果你打算多次使用它。

在WPF/Silverlight中,Button控件有其Style的过度定义。因此,每当您尝试更改某些内容时,例如Background,某些已经定义的Triggers将覆盖自定义样式。就像你经历过的那样。

为了克服这个问题,您需要为按钮创建自己的ControlTemplate,在其中您可以显式地定义它的Background,并且在禁用时永远不要添加Trigger来更改它。