如果触发,则覆盖按钮BackGround颜色

时间:2016-12-14 09:41:52

标签: wpf

我有一个按钮样式,用于我的所有按钮。它包含一个带有触发器的部件IsEnabled

        <Style x:Key="FlatButton" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="#4CAF50"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="10 5"/>
            <Setter Property="FontSize" Value="14" />
            <Setter Property="BorderThickness" Value="2" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Border
                            x:Name="Border"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" />

                            <ContentPresenter
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            Margin="{TemplateBinding Padding}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            RecognizesAccessKey="True" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="White"/>
                                <Setter Property="Background" Value="{StaticResource MaterialGrey}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

由于它们的用途,每个按钮都有自己的颜色,如:

<Button Background="Green"/>

由于这个setter,在IsEnabled = False时,Background的颜色不会改变。 当IsEnabled = False时,有没有办法覆盖这种颜色?因此,无论原始背景颜色如何,我的所有按钮在禁用时都将显示为灰色。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

Style setter无法设置具有本地值的属性,但您可以将ControlTemplate中Setter的TargetName设置为&#34; Border&#34;更改Border元素的背景:

    <Style x:Key="FlatButton" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#4CAF50"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="10 5"/>
        <Setter Property="FontSize" Value="14" />
        <Setter Property="BorderThickness" Value="2" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Border
                        x:Name="Border"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}" />

                        <ContentPresenter
                         x:Name="cp"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        Margin="{TemplateBinding Padding}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        RecognizesAccessKey="True" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="cp" Property="TextBlock.Foreground" Value="White"/>
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource MaterialGrey}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
 <Button Background="Blue" Content="Button" Style="{StaticResource FlatButton}" IsEnabled="False" />

答案 1 :(得分:0)

将您的Background转移到Style,如下例所示:

 <ControlTemplate>
  <ControlTemplate.Resources>
     <Style TargetType={...}>
        <Setter Property="Background" Value="Green"/>
     </Style>
  </ControlTemplate.Resources>
  <ControlTemplate.Triggers>
     <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Background" Value="Gray"/>
     </Trigger>
  </ControlTemplate.Triggers>
  ...
 </ControlTemplate>

如果您不想将Background转移到Style,请使用ColorAnimation中的Trigger更改此answer Foreground }。