Setter 对于 ContentPresenter 内的元素

Setter for elements inside ContentPresenter

我有一个 ControlTemplate 这样的:

<ControlTemplate TargetType="Button">
    <Grid Background="Transparent">
        <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center"
                          VerticalAlignment="Center" />
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect BlurRadius="20" Color="Black" ShadowDepth="0"
                                      Opacity="0.5" />
                </Setter.Value>
            </Setter>
            <Setter Property="FontWeight" Value="SemiBold"/>
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect BlurRadius="20" Color="Black" ShadowDepth="0" Opacity="1" />
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="Gray" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

当 IsMouseOver 属性 变为 True 时,我希望 contentPresenter 内的所有图像都具有 DropShadowEffect,而不是按钮本身。
我尝试将 setter 属性 更改为 Image.Effect 但它不起作用。
我该怎么做?

这是鼠标悬停时我的按钮的样子:
这就是它不存在时的样子:
如您所见,Image 和 TextBlock 都获得了阴影效果。但我只想让图像得到它。

您真的不需要定义 Button 模板,而是创建一个 Image 样式

<Style x:Key="style"
       TargetType="Image">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Button}}"
                     Value="True">
            <DataTrigger.Setters>
                <Setter Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect BlurRadius="20"
                                          Color="Black"
                                          ShadowDepth="0"
                                          Opacity="0.5" />
                    </Setter.Value>
                </Setter>
            </DataTrigger.Setters>
        </DataTrigger>
   </Style.Triggers>

并将其应用到您在按钮中显示的每个图像

<Button>
...
    <Image Style="{StaticResource style}" ... />
...
</Button>

您可以采用 Sinatr 推荐的样式并将其放入按钮控件模板的 ControlTemplate.Resources 部分。这是一个对我有用的例子。它还处理 IsPressed 事件,在此期间我假设您只想调整图像的投影效果:

<ControlTemplate x:Key="BtnTemplate" TargetType="{x:Type Button}">
    <Grid Background="Transparent">
        <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>

    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="FontWeight" Value="SemiBold"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="Gray" />
        </Trigger>
    </ControlTemplate.Triggers>

    <ControlTemplate.Resources>
        <Style TargetType="{x:Type Image}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
                    <DataTrigger.Setters>
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect BlurRadius="20" Color="Black" ShadowDepth="0" Opacity="0.5"/>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger.Setters>
                </DataTrigger>

                <DataTrigger Binding="{Binding IsPressed, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
                    <DataTrigger.Setters>
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect BlurRadius="20" Color="Black" ShadowDepth="0" Opacity="1" />
                            </Setter.Value>
                        </Setter>
                    </DataTrigger.Setters>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ControlTemplate.Resources>
</ControlTemplate>