shawn a
shawn a

Reputation: 809

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

I'm new to WPF and XAML. This question is pretty straight forward.

I want to modify the ellipse's Fill property from the trigger from its default "#597E0000" to "Black" when the button is pressed. Clearly I am not accessing the property correctly because i can't compile it and get the following on the line indicated in the code below:

"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>

Upvotes: 0

Views: 864

Answers (1)

kennyzx
kennyzx

Reputation: 12993

If you define the Trigger in the ControlTemplate, you have access to the elements in the ControlTemplate.

See also 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>

Upvotes: 1

Related Questions