如何在按钮悬停时为按钮的子节点加下划线

时间:2017-06-22 23:39:56

标签: c# wpf triggers

我有以下风格

 <Style x:Key="RoundedBtn" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="BorderBrush" Value="{x:Null}"/>

            <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="button" CornerRadius="5"  Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid>
                                <Image x:Name="image" Source="{Binding ButtonImage, RelativeSource={RelativeSource AncestorType=Button}}" Margin="0,2,0,3" HorizontalAlignment="Left" Width="40" />
                                <Border BorderBrush="{x:Null}"  Height="16" Margin="45,15,0,14"  Width="123" HorizontalAlignment="Left">
                                    <TextBlock x:Name="textBlock" Margin="-1" TextWrapping="Wrap" Text="{Binding ButtonText, RelativeSource={RelativeSource AncestorType=Button} }"
                                               FontSize="13"  
                                               FontFamily="{DynamicResource Helvetica}" VerticalAlignment="Center" TextDecorations="{x:Null}">
                                    </TextBlock>
                                </Border>
                            </Grid>
                        </Border>
                            <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" Value="#8da0aa"></Setter>
                                <Setter Property="Foreground" Value="#318EE4"></Setter>
                                <Setter TargetName="textBlock" Property="TextBlock.TextDecorations" Value="Underline">
                                <!--That Text decoration part doesnt work--></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                    </Setter.Value>
                </Setter>

        </Style>

我将此样式应用于我的“usercontrol”(实际上是一个按钮):

<Button x:Name="btn" x:Class="MyStock.MyButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d" Height="45" Width="183" Style="{DynamicResource RoundedBtn}" />

当我生成项目时,“MyButton”出现在我的自定义控件中。但是如果我将它拖到我的表单上进行测试,那么Text Underline就不起作用了。

但是,如果我在表单上放一个普通的按钮,然后应用RoundedBtn样式,它可以工作,为什么?

顺便说一句,我想要实现的是在this page

上设置一个看起来像左侧菜单按钮的按钮

0 个答案:

没有答案