Can't get underline to show up when triggered

青春壹個敷衍的年華 提交于 2019-12-11 03:23:52

问题


I have the following style, but when the mouse over trigger is true, no underline shows up on the text.

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

回答1:


This might not be an ideal solution, but you could define the trigger in your control template. Don't forget to reference your TextBlock with the TargetName property on the setter.

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


The TextDecorations property is not inherited so setting the value on the Button (which is what your trigger is targeting), will not accomplish what you want. You can probably use a StoryBoard to do that, but I can't remember if a storyboard can target an element defined in a template (only way to find out is to do it).



来源:https://stackoverflow.com/questions/13751235/cant-get-underline-to-show-up-when-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!