如何触发样式到 ContentPresenters 里面

How can I trigger style to ContentPresenters inside

我想为 ContentPresenter 的项目触发样式。 我该怎么做?

我有这样的模板。 (几乎默认的按钮样式)

<Style x:Key="ImageButtonStyle2" TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Background" Value="{StaticResource Button.Static.Background3}"/>
    <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border3}"/>
    <Setter Property="Foreground" Value="{StaticResource Button.Static.Foreground3}"/>
    <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="{StaticResource Button.Static.Foreground3}"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background3}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border3}"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background3}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border3}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background3}"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border3}"/>
                        <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground3}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

而且,我正在使用这样的按钮。

<Button x:Name="ImageButton2" Style="{DynamicResource ImageButtonStyle2}">
    <StackPanel Orientation="Vertical">
        <Viewbox Width="48.000" Height="30.000">
            <Canvas Width="48.000" Height="30.000">
                <Path Fill="..." Data="..." />
            </Canvas>
        </Viewbox>
        <TextBlock Text="XXX"/>
    </StackPanel>
</Button>

禁用按钮时,我想更改路径和文本块项目的填充、前景和背景颜色。

我想写一个 Setter 标签到触发器“IsEnable”标签...但我坚持这样做。

有什么好的方法吗?

如果你想要 Button 的相同背景颜色反映在你的 Path 和 TextBlock 中,你可以 Bind Background 属性 of ButtonPathFillTextBlockForeground 属性.

<Button x:Name="ImageButton2" Height="30" Width="100">
    <StackPanel Orientation="Vertical">
        <Viewbox Width="48.000" Height="30.000">
            <Canvas Width="48.000" Height="30.000">
                <Path Fill="{Binding Path=Background, RelativeSource={RelativeSource AncestorType=Button}}"
                      Data="..." />
            </Canvas>
        </Viewbox>
        <TextBlock Foreground="{Binding Path=Background, RelativeSource={RelativeSource AncestorType=Button}}"
                   Text="XXX"/>
    </StackPanel>
</Button>

或者

如果您正在寻找不同的 Backgroud,您可以尝试使用单独的 Triggers 设置前景。喜欢,

<TextBlock Text="XXX">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Green"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType=Button}}" 
                             Value="False">
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>