2

How can I Underline the Text of the ToggleButton when mouse is over it? I'm using the below code but nothing happens when I hover the mouse.

<Style x:Key="BaseToggleButton" TargetType="ToggleButton">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="TextOptions.TextFormattingMode" Value="Display"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Margin" Value="0 3 0 3"/>
    <Setter Property="FontSize" Value="11"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <StackPanel Orientation="Horizontal" x:Name="Border">
                    <Image Width="13" Height="13" Source="{StaticResource ColumnsLayoutMiniIcon}">
                    </Image>
                    <Border x:Name="Content"
                            Padding="4 2 4 2"
                            Margin="5 0 0 0">
                        <ContentPresenter HorizontalAlignment="Center" 
                                          VerticalAlignment="Center" 
                                          RecognizesAccessKey="True" />

                    </Border>                        
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Cursor" Value="Hand"/>
                        <Setter TargetName="Content" Property="TextBlock.TextDecorations" Value="Underline"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="False">

                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="Content" Property="Background" Value="#acacac"></Setter>
                        <Setter TargetName="Content" Property="TextBlock.Foreground" Value="Black"></Setter>
                    </Trigger>
                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This is odd because this part of the code works:

<Setter TargetName="Border" Property="Cursor" Value="Hand"/>
Vahid
  • 5,144
  • 13
  • 70
  • 146
  • 2
    Check out this [link](http://stackoverflow.com/questions/13751235/cant-get-underline-to-show-up-when-triggered) which shows an interesting way to achieve underline for a ContentPresenter as a whole by encasing the ContentPresenter inside a TextBlock and then using the TextBlock as the target of the trigger, thus underlining the entire ContentPresenter. – learningcs Jan 06 '15 at 21:52
  • Thanks the link you provided helped me solve the problem. – Vahid Jan 06 '15 at 23:18

1 Answers1

2

If you want to set the TextDecorations="Underline" in Style, then you need to use the TextBlock instead of ContentPresenter (or any other control).

Yes, it is dependency property, but it is not attached proprety, is plain dependency property defined and exposed by TextBlock and it is is perfectly visible in ILSpy:

TextBlock.TextDecorationsProperty = Inline.TextDecorationsProperty.AddOwner(typeof(TextBlock),
                                    new FrameworkPropertyMetadata(new FreezableDefaultValueFactory(TextDecorationCollection.Empty), 
                                    FrameworkPropertyMetadataOptions.AffectsRender));

Therefore, this property can only be set to control the type of TextBlock.

Community
  • 1
  • 1
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68