WPF已禁用按钮的背景

时间:2014-08-20 14:07:58

标签: wpf xaml styles

我尝试在禁用时更改按钮的样式:

    <Style TargetType="Button" x:Key="MyButton2">
        <Setter Property="Background" Value="MediumAquamarine" />
        <Setter Property="Foreground" Value="MediumBlue" />

        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Background" Value="Green"/>
                <Setter Property="Foreground" Value="DeepPink"/>
            </Trigger>
        </Style.Triggers>

    </Style>

和我的按钮:

<Button Style="{StaticResource MyButton2}" Content="My text" Width="100" Height="30" IsEnabled="False" />    

但由于某种原因,此样式不适用于按钮:

enter image description here

如何将此样式应用于我的按钮?我可以不用覆盖按钮的模板而只使用样式吗?

6 个答案:

答案 0 :(得分:23)

  

我是否可以不使用样式覆盖按钮的模板?

我认为不是,因为Windows中的控件在每个版本的Windows中都有默认的StylesControlTemplates以及,它们是不同的。另外,样式 - 它只是很多设置,通常样式不会改变/添加行为来控制,这是负责ControlTemplate的。

Note:样式它是一组设置器,例如:设置背景,大小等。ControlTemplate是表单,其中将显示所有这些设置。

在这种情况下,我建议大家更改ControlTemplate,无论系统版本如何,都可以在视图中使用相同的行为。

在这种情况下,请尝试此Style

<Window.Resources>
    <Style TargetType="Button" x:Key="MyButton2">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Background" Value="MediumAquamarine" />
        <Setter Property="Foreground" Value="MediumBlue" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter x:Name="MyContentPresenter" 
                                          Content="{TemplateBinding Content}"
                                          HorizontalAlignment="Center" 
                                          VerticalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="Green"/>
                <Setter Property="Foreground" Value="DeepPink"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <Button Content="Button" 
            IsEnabled="False"
            HorizontalAlignment="Left" 
            VerticalAlignment="Top" 
            Width="75" 
            Style="{StaticResource MyButton2}"
            Click="Button_Click"/>
</Grid>

答案 1 :(得分:7)

奇怪的是,当我运行它时,它“工作”,部分原因,这实际上可能是按钮的模板。这是一个按钮整个模板。

<Style x:Key="FocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
    <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsDefaulted" Value="true">
                        <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                        <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

正如您所看到的,按钮的控件模板中发生了很多事情。例如,在禁用ctl时使用以下SolidColorBrushes。

<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>

您可以更改这些(在您的用户控件范围内,应用程序),或者您可以将控制模板重写为Daniel Ward建议的。

您可以在UI编辑器中右键单击任何控件的默认模板,编辑模板 - &gt;创建副本......通过混合,代码或反汇编:)

希望它有所帮助!

干杯

了Stian

答案 2 :(得分:2)

似乎唯一的方法是从按钮中删除chrome主题。 请注意,如果您正在为此进行构建,这将删除悬停/点击动画以及Windows 7上按钮的渐变。

不幸的是,Microsoft决定使用disabling a button should give the background a hardcoded value from a private property in the chrome theme's code-behind,这意味着您无法通过XAML进行更改。该链接描述了问题:

Button的控件模板使用ButtonChrome,其中包含名为ButtonOverlay的私有属性,当Button IsEnabled设置为{{{}时1}},将False设置为Background(或RGB 244,244,244),这是您看到的灰色。你无法改变它。

您可以做的是为按钮创建一个新的控件模板(yay),将new SolidColorBrush(Color.FromRgb(0xf4, 0xf4, 0xf4))元素替换为Themes:ButtonChrome元素,您可以更改Border的{​​{1}}元素

它可能看起来不适用于设计视图,但在运行时可以正常工作。

因此,例如,对于你的,它可能看起来像:

Background

答案 3 :(得分:1)

我的解决方案要容易一些,但是它不允许为禁用的按钮使用Transparent背景色,而且您也不能更改BorderBrush。 我要做的是使用TextBlock作为按钮的内容,并在禁用该TextBlock背景时更改其颜色。

我的按钮是这样写的:

<Button>
    <TextBlock Text="Click me!" Style="{StaticResource MyButtonContentStyle}" />
</Button>

MyButtonContentStyle是这样写的:

<Style x:Key="MyButtonContentStyle" TargetType="TextBlock">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="White" />
        </Trigger>
    </Style.Triggers>
</Style>

这对我们来说就像是一种魅力。

答案 4 :(得分:1)

这可能要晚了,但是WPF提供了一个内置属性:

IsHitTestVisible="False"

使用上面的控件无法检测到任何类型的鼠标移动,因此在不更改控件实际外观的情况下将其禁用。

答案 5 :(得分:1)

根据工具栏按钮样式添加。这个解决方案对我有用

BasedOn="{​StaticResource {​x:Static ToolBar.ButtonStyleKey}​}​"

相关问题