1

我有一个按钮,它有一个控件模板集(包括一些控件模板触发器),我想在 DataTrigger 上更改它的文本颜色。我读过我无法访问模板的 TargetName(“theContent”),因为它有自己的范围,那么我该如何实现呢?

  <Button BorderThickness="0" MaxWidth="94" Height="26" Margin="1,0,0,0">
        <Button.Style>
            <Style TargetType="Button">                        
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border x:Name="theBorder" CornerRadius="15" BorderThickness="0" Background="{TemplateBinding Background}">
                                <Label x:Name="theContent" VerticalContentAlignment="Center" Foreground="White" FontWeight="Bold">
                                    <TextBlock Text="..." TextTrimming="CharacterEllipsis"></TextBlock>
                                </Label>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="False">
                                    <Setter Property="Background" Value="#1A65AF"></Setter>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#4d87c0"></Setter>
                                </Trigger>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter Property="Background" Value="#004b95"></Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{...}" Value="..."></Condition>
                            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="False"></Condition>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Background" Value="#ad4811"/>
                        <Setter Property="Foreground" Value="#7F7F7F" /> // does nothing
                        <Setter TargetName="theContent" Property="Foreground" Value="#7F7F7F" /> // target not found
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
        <!--<Button.Content>
            <Label x:Name="theContent" VerticalContentAlignment="Center" Foreground="White" FontWeight="Bold">
                <TextBlock Text="..." TextTrimming="CharacterEllipsis"></TextBlock>
            </Label>
        </Button.Content>-->
    </Button>

我尝试过但不起作用的两个二传手是:

<Setter Property="Foreground" Value="#7F7F7F" /> // does nothing
<Setter TargetName="theContent" Property="Foreground" Value="#7F7F7F" /> // target not found
4

1 回答 1

0

在您的控件模板中,您只需要使用TemplateBinding而不是将值硬编码为White.

<Border x:Name="theBorder" CornerRadius="15" BorderThickness="0" Background="{TemplateBinding Background}">
  <Label x:Name="theContent" VerticalContentAlignment="Center" Foreground="{TemplateBinding Foreground}" FontWeight="Bold">
    <TextBlock Text="..." TextTrimming="CharacterEllipsis"></TextBlock>
  </Label>
</Border>

这样做将遵循按钮Foreground属性。通常,如果您的任何属性需要ControlTemplate动态更改,您将需要使用TemplateBinding(例如FontWeight属性)。现在您的StyleTrigger二传手将按预期工作。

顺便说一句,Foreground="{TemplateBinding Foreground}"只是简写

Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
于 2018-11-01T17:56:32.003 回答