Setter TargetName ' contentpresent '在触发器中无法识别

本文关键字:识别 contentpresent TargetName Setter 触发器 | 更新日期: 2023-09-27 18:07:34

我有一个ContentPresenter,当按钮被禁用时,内容的不透明度应该变成0.5,但是Setter不能通过TargetName找到它。我是不是漏掉了什么?

<Style TargetType="Button">
    <Style.Triggers>
        <Trigger Property="Tag" Value="MenuButton">
            <Setter Property="Background" Value="{StaticResource BackgroundBrush}"/>
            <Setter TargetName="ContentPresenter" Property="Opacity" Value="1.0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border BorderThickness="1,0,0,1" Background="{TemplateBinding Background}">
                            <ContentPresenter
                                x:Name="ContentPresenter"
                                Margin="1"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"
                                Opacity="1.0"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
            <Setter TargetName="ContentPresenter" Property="Opacity" Value="0.5"/>
        </Trigger>
     </Style.Triggers>
</Style>

Setter TargetName ' contentpresent '在触发器中无法识别

对于此类问题,您需要了解NameScope的概念。Template定义自己的NameScope。因此,您的TargetName将无法从模板外部工作。

那么,试着从ControlTemplate内部访问它

你可以使用ControlTemplate.Triggers来改变ContentPresenter的属性。"ContentPresenter"的名称只有在模板中才知道。

<Style TargetType="Button">
    <Style.Triggers>
        <Trigger Property="Tag" Value="MenuButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border BorderThickness="1,0,0,1" Background="{TemplateBinding Background}">
                            <ContentPresenter
                                x:Name="ContentPresenter"
                                Margin="1"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"
                                Opacity="1.0"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                                <Setter TargetName="ContentPresenter" Property="Opacity" Value="0.5"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="True">
                                <Setter Property="Background" Value="{StaticResource BackgroundBrush}"/>
                                <Setter TargetName="ContentPresenter" Property="Opacity" Value="1"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>