Silverlight:如何让超链接按钮看起来像文本块?

3

我正在尝试使文本块中的所有URI链接可点击。这是我采取的方法:

    private static void onTextChanged(DependencyObject dependObj, DependencyPropertyChangedEventArgs e)
    {
        WrapPanel wrapPanel = ((HyperlinkTextBlock)dependObj).LayoutRoot;
        wrapPanel.Children.Clear();

        // TODO: use a real wordbreaker?
        // adding an extra space to the end of the last word. Cry.
        IList<string> words = ((string)e.NewValue).Split(' ').Select(word => word + " ").ToList();
        foreach (string word in words)
        {
            Uri uri;
            if (Uri.TryCreate(word, UriKind.Absolute, out uri))
            {
                // TODO the style is off - the text is too big
                wrapPanel.Children.Add(new HyperlinkButton()
                {
                    Content = word,
                    NavigateUri = uri,
                    TargetName = "_blank",
                    Margin = new Thickness(0),
                    Padding = new Thickness(0),
                });
            }
            else
            {
                wrapPanel.Children.Add(new TextBlock() { Text = word, TextWrapping = TextWrapping.Wrap });
            }
        }
    }

我完全支持使用更加面向XAML/声明式的方式来实现这个,但我不确定如何去做。
这个方法可以正常工作(除了最好能够使用真正的分词器之外),但是HyperlinkButton看起来很奇怪。它太大了,文本无法换行。它似乎也有一些偏移,我试图通过将MarginPadding设置为0来修复它,但是没有解决问题。
还有其他想法吗?实际上,我想要HyperlinkText而不是HyperlinkButton,但我不认为Windows Phone 7的Silverlight 3提供了这个功能。
3个回答

2
这是我想出的解决方案,通过提取超链接按钮的样式,并对我关心的区域进行修改(就像你提到的奇怪缩进)。这样,行为方式完全相同,除了您想要更改的内容。
创建超链接按钮时,还应设置样式属性,如下所示:
var button = new HyperlinkButton
{
    ...
    Style = (Style) Resources["HyperlinkButtonWithNoIndent"]
};

然后在您的页面中,将以下样式添加到资源中:

<Style x:Key="HyperlinkButtonWithNoIndent" TargetType="HyperlinkButton">
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="HyperlinkButton">
                <Border Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="0.5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="TextElement"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="TextElement">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                        <TextBlock x:Name="TextElement" Text="{TemplateBinding Content}" TextDecorations="Underline" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1

我从未尝试过在 TextBlock 中实现这个,但是在 ImageStackPanel 中可以使用,所以在你的情况下应该也可以。为你的 TextBlock 添加一个 Tap 手势处理程序并监听它。然后,在事件处理程序中,你可以导航到 URL。

TextBlock MyTextBlock = new TextBlock() { Text = "Tap Me!" };
GestureListener listener = GestureService.GetGestureListener( MyTextBlock );
listener.Tap += new EventHandler<GestureEventArgs>( OnMyTextBlockTapped );

事件处理程序看起来像这样:

void OnMyTextBlockTapped( object sender, GestureEventArgs e )
{
  // Navigate to URL
}

你甚至可以通过在事件处理程序中启动一个Storyboard并在动画完成时执行导航来使点击动画化。

将轻拍手势添加到文本块同样可行。 - Mick N

0

你尝试在 Blend 中重新模板化控件了吗?在那里,你可以完全控制组成 HyperlinkButton 的所有部分的演示样式。

  1. 右键单击控件
  2. 编辑模板
  3. 编辑副本
  4. 格式化

你可以重复使用所得到的样式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接