2012-12-06 44 views
2

我有以下样式,但当鼠标悬停在触发器上时,文本上没有下划线显示。触发时无法下划线显示

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

回答

3

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

<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> 
+0

这绝对是一个更好的方式来做到这一点。 –

+0

感谢您的帮助,它工作正常。 – wade

1

TextDecorations property没有继承这样设置的按钮的值(这是你触发的目标是什么),也不会完成你想要的。您可以使用StoryBoard来做到这一点,但我不记得故事板是否可以定位模板中定义的元素(只有这样才能发现它)。