如何在Silverlight中为HyperlinkBut​​ton添加下划线?

时间:2010-06-29 13:59:14

标签: c# silverlight hyperlink

似乎我可以像格式化TextBlock一样格式化HyperlinkBut​​ton:

HyperlinkButton hyperlinkButton = new HyperlinkButton();
hyperlinkButton.Content = "google";
hyperlinkButton.NavigateUri = new Uri("http://www.google.com");
hyperlinkButton.TargetName = "blank";
hyperlinkButton.Foreground = XamlHelpers.GetColorFromHex("555");
hyperlinkButton.TextDecoration = ... //error
hyperlinkButton.FontWeight = FontWeights.Bold;

但是,TextDecoration不像在TextBlock中那样工作。我在鼠标悬停时获得了自动下划线,但希望在鼠标悬停之前也有下划线。

如何在Silverlight中为HyperlinkBut​​ton添加下划线?

2 个答案:

答案 0 :(得分:15)

如果您只需要静态下划线(没有鼠标悬停效果),您应该只使用TextBlock作为HyperlinkBut​​ton的内容。由于HyperlinkBut​​ton是一个ContentControl,它可以采用任何其他控件类型作为内容(不仅仅是简单的字符串)。

以下是一些XAML,它将为您提供带下划线的TextBlock作为HyperlinkBut​​ton的内容:

    <HyperlinkButton NavigateUri="http://google.com">
        <TextBlock Text="Google" TextDecorations="Underline" />
    </HyperlinkButton>

您应该能够创建TextBlock,并在代码隐藏中使用C#设置HyperlinkBut​​ton的Content属性,如果这就是您正在做的事情。

正如大卫所说,编辑ControlTemplate肯定能够根据自己的喜好设计HyperlinkBut​​ton样式,但使用实际带下划线的TextBlock作为内容可能更简单,并且如果你需要做的话,可以少得多的XAML。

答案 1 :(得分:5)

编辑控件模板。我通过Expression Blend撕掉了它。

 <Style x:Key="HyperlinkButtonStyle1" TargetType="HyperlinkButton">
            <Setter Property="Foreground" Value="#FF73A9D8"/>
            <Setter Property="Padding" Value="2,0,2,0"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="HyperlinkButton">
                        <Grid Background="{TemplateBinding Background}" Cursor="{TemplateBinding Cursor}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="UnderlineTextBlock">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="UnderlineTextBlock">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOverlay">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualElement"/>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unfocused"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <TextBlock x:Name="UnderlineTextBlock" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Text="{TemplateBinding Content}" TextDecorations="Underline" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>                       
                            <TextBlock x:Name="DisabledOverlay" Foreground="#FFAAAAAA" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Text="{TemplateBinding Content}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Canvas.ZIndex="1"/>
                            <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Opacity="0" Stroke="#FF6DBDD1" StrokeThickness="1"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

更改此控件的可见性:

<TextBlock x:Name="UnderlineTextBlock" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Text="{TemplateBinding Content}" TextDecorations="Underline" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>