如何更改 ContentPresenter 颜色 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17134224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:01:00  来源:igfitidea点击:

How to change ContentPresenter Color WPF

wpfexpression-blend

提问by Dimitri

I have a custom button in WPF. On disabled state I want it's content color to change, but I can't manage to do that. I'm using Blend for Visual Studio. When I go to edit template and choose contentPresente's brush color it says that value is invalid. How can I do that ? I tried to change it in XAML and this is the code I used but there is an error.

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

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

回答by Viv

ContentPresenterby itself does not have any Visual Style's. It's merely a placeholder for the Contentof the Controlin the Stylewhich it resides in.

ContentPresenter本身没有任何 Visual Style。它只是它所在ContentControlin 的占位符Style

To modify the Foregroundin your custom Button's Style.ControlTemplate, you could use the attached property TextElement.Foregroundon the ContentPresenterwith a Triggerfor IsEnabled="False"

要修改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>
...

You can accordingly do the same via Storyboard's or the VisualStateManagervia blend for the same property.

您可以相应地对同一属性执行相同的 viaStoryboardVisualStateManagervia blend 。

Note:

笔记:

Very important to understand that by doing this^^ we are assuming the Buttonusing this Styleto have its Contentas some Text. If your Button.Contentis something else like another FrameworkElement(say a ComboBox) You will not see it become "Red".

理解这样做非常重要^^ 我们假设Button使用 thisStyle将其Content作为一些文本。如果你Button.Content的东西像另一个FrameworkElement(比如 a ComboBox),你不会看到它变成“红色”。

回答by Dave_cz

Found a "Hacky" way to do this for any element by using ControlTemplate.Resourcesand TargetType:

通过使用ControlTemplate.Resourcesand找到了对任何元素执行此操作的“Hacky”方法TargetType

<ControlTemplate x:Key="GlyphButton" TargetType="{x:Type Button}">
  <ControlTemplate.Resources>
    <Style TargetType="{x:Type fa:ImageAwesome}">
        <Setter Property="Foreground" Value="Gray"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsMouseOver, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Value="true">
                <Setter Property="Foreground" Value="#FF048758"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
  </ControlTemplate.Resources>
  <ContentPresenter/>
</ControlTemplate>

Sadly you can't use button's VisualStateManagerthis way.

遗憾的是,您不能以VisualStateManager这种方式使用按钮。