Vahid
Vahid

Reputation: 5444

Underline the text of the ToggleButton OnMouseOver in WPF

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

Upvotes: 2

Views: 1350

Answers (1)

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

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.

Upvotes: 2

Related Questions