【问题标题】:Can't get underline to show up when triggered触发时无法显示下划线
【发布时间】:2023-04-06 05:30:01
【问题描述】:

我有以下样式,但是当鼠标悬停触发器为真时,文本上不显示下划线。

<Style x:Key="HyperlinkToggleButtonStyle" TargetType="ToggleButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <TextBlock x:Name="TextBlock">
                            <ContentPresenter Content="{TemplateBinding  Content}" ContentTemplate="{TemplateBinding  ContentTemplate}"/>
                </TextBlock>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="false">
            <Setter Property="Background" Value="{StaticResource StandardBackground}"/>
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="FontStyle" Value="Normal"/>
            <Setter Property="FontWeight" Value="Normal"/>                                         
        </Trigger>

        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="{StaticResource StandardBackground}"/>
            <Setter Property="Foreground" Value="{StaticResource StandardBlue}" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="FontStyle" Value="Normal"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="TextBlock.TextDecorations" Value="Underline"/>
        </Trigger>
    </Style.Triggers>
</Style>

【问题讨论】:

    标签: wpf xaml triggers underline text-decorations


    【解决方案1】:

    这可能不是一个理想的解决方案,但您可以在控件模板中定义触发器。不要忘记使用 setter 上的 TargetName 属性引用您的 TextBlock。

        <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="ToggleButton">
                  <TextBlock x:Name="TextBlock">
                            <ContentPresenter Content="{TemplateBinding  Content}" ContentTemplate="{TemplateBinding  ContentTemplate}"/>
                  </TextBlock>
                  <ControlTemplate.Triggers>
                     <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="TextBlock" Property="TextBlock.TextDecorations" Value="Underline"/>
                     </Trigger>
                  </ControlTemplate.Triggers>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
    

    【讨论】:

    • 这绝对是一个更好的方法。
    【解决方案2】:

    TextDecorations property 没有被继承,因此在 Button 上设置值(这是您的触发器所针对的)不会实现您想要的。您可能可以使用 StoryBoard 来做到这一点,但我不记得 Storyboard 是否可以针对模板中定义的元素(唯一的方法就是这样做)。

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多