WPF - 在 XAML 中从该按钮的触发器修改按钮的椭圆对象

WPF - Modify button's ellipse object from that button's trigger in XAML

我是 WPF 和 XAML 的新手。这个问题很直接。

我想在按下按钮时将触发器的椭圆填充 属性 从默认的“#597E0000”修改为 "Black"。显然我没有正确访问 属性 因为我无法编译它并在下面的代码指示的行上获得以下内容:

"TargetName property cannot be set on a Style Setter. Line 16"

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ExitButton" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate x:Name="exitButtonTemplate" TargetType="Button">
                <Grid>
                    <Ellipse x:Name="exitButtonEllipse" Fill="#597E0000"/>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Setter TargetName="exitButtonEllipse" Property="Fill" Value="Black" /> <!-- error -->
        </Trigger>
    </Style.Triggers>
</Style>

如果您在ControlTemplate 中定义了Trigger,您就可以访问ControlTemplate 中的元素。

另见 ControlTemplate.Triggers Property

<Style x:Key="ExitButton" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate x:Name="exitButtonTemplate" TargetType="Button">
                <Grid>
                    <Ellipse x:Name="exitButtonEllipse" Fill="#597E0000"/>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="exitButtonEllipse" Property="Fill" Value="Black" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>