如何更改ContentPresenter Color WPF

Dim*_*tri 3 wpf expression-blend

我在WPF中有一个自定义按钮.在禁用状态下,我希望它的内容颜色可以改变,但我无法做到这一点.我正在使用Blend for Visual Studio.当我去编辑模板并选择contentPresente的画笔颜色时,它表示该值无效.我怎样才能做到这一点 ?我试图在XAML中更改它,这是我使用的代码,但有一个错误.

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter">
    <EasingColorKeyFrame KeyTime="0" Value="Black" />
</ColorAnimationUsingKeyFrames>
Run Code Online (Sandbox Code Playgroud)

Viv*_*Viv 10

ContentPresenter本身没有任何Visual Style.它只是为一个占位符ContentControlStyle其驻留英寸

要修改Foreground您的自定义ButtonStyle.ControlTemplate,你可以使用附加属性TextElement.ForegroundContentPresenter有一个Trigger用于IsEnabled="False"

...
<ControlTemplate.Triggers>
  ...
  <Trigger Property="IsEnabled"
            Value="False">
    <Setter TargetName="contentPresenter"
            Property="TextElement.Foreground"
            Value="Red" />
  </Trigger>
</ControlTemplate.Triggers>
...
Run Code Online (Sandbox Code Playgroud)

因此,您可以为同一属性执行相同的via StoryboardVisualStateManagervia混合.

注意:

非常重要的是要理解,通过这样做^^我们假设Button使用它Style来使其Content作为一些文本.如果你Button.Content的其他东西像另一个FrameworkElement(比如说a ComboBox),你就不会看到它变成"红色".