我正在尝试使textblock中所有可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不能提供这种功能。

最佳答案

这是我提出的解决方案,方法是提取超链接按钮的样式,然后对我关心的区域进行修改(如您提到的那样,是奇数个缩进)。这样,除了要更改的内容外,其他行为是完全相同的。

当创建超链接按钮时,还设置样式属性,如下所示:

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>

10-06 13:06