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

时间:2015-01-10 03:09:20

标签: .net wpf xaml button

我是WPF和XAML的新手。这个问题很简单。

我想从触发器的默认值"#597E0000"中修改椭圆的Fill属性。到"黑"当按下按钮时。很明显,我没有正确访问该属性,因为我无法编译它并在下面的代码中指示的行上获得以下内容:

"无法在样式设置器上设置TargetName属性。第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>

1 个答案:

答案 0 :(得分:1)

如果在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>