3

WPF にカスタム ボタンがあります。無効な状態で、コンテンツの色を変更したいのですが、それができません。Blend for Visual Studio を使用しています。テンプレートを編集して contentPresente のブラシの色を選択すると、値が無効であると表示されます。どうやってやるの ?XAMLで変更しようとしましたが、これは私が使用したコードですが、エラーがあります。

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

2 に答える 2

11

ContentPresenter自体には Visual はありませんStyleContentそれが存在するのControlの単なるプレースホルダですStyle

ForegroundカスタムButtonのを変更するには、forで添付プロパティをStyle.ControlTemplate使用できますTextElement.ForegroundContentPresenterTriggerIsEnabled="False"

...
<ControlTemplate.Triggers>
  ...
  <Trigger Property="IsEnabled"
            Value="False">
    <Setter TargetName="contentPresenter"
            Property="TextElement.Foreground"
            Value="Red" />
  </Trigger>
</ControlTemplate.Triggers>
...

それに応じて、同じプロパティに対して同じビアStoryboardまたはVisualStateManagerビア ブレンドを実行できます。

ノート:

これを行うことで理解することが非常に重要です^^これをButton使用してテキストとしてStyle持つことを前提としています。ContentあなたButton.Contentが別のもののようなものである場合FrameworkElement(aと言うComboBox)、それが「赤」になるのを見ることはありません。

于 2013-06-16T15:47:53.883 に答える
1

ControlTemplate.Resourcesandを使用して、任意の要素に対してこれを行う「ハッキーな」方法を見つけました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>

残念ながら、この方法ではボタンを使用できませんVisualStateManager

于 2016-06-23T19:56:19.527 に答える