在WPF中去除动态超链接的下划线

15

我创建了一个WPF应用程序。在某个表单中,用户可以将选定的richtextbox文本更改为超链接。我花了一个多小时搜索解决方案,但是找不到。

我的动态超链接是按以下方式创建的:

                var textRange = RichTextBox.Selection;
                textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);

                var hyperlink = new Hyperlink(textRange.Start, textRange.End)
                {
                    IsEnabled = true,
                    Foreground = Brushes.Blue
                };

                hyperlink.NavigateUri = new Uri("http://search.msn.com/" + firstOrDefault.WordId);
                var main = new WordMain();
                hyperlink.Click += new RoutedEventHandler(main.hyperLink_Click);
                RichTextBox.IsDocumentEnabled = true;
                RichTextBox.IsReadOnly = false;
如何去除动态超链接的下划线。我想使用文本装饰,但无法通过代码实现。

很抱歉,有一件事我不太明白:您是否需要保留其他的TextDecoration?或者您可以全部删除吗? - appa yip yip
@Elvin Mammadov:你的应用程序中所有超链接都应该具有相同的格式吗? - Pollitzer
是的。我将这些超链接添加到数据库中,使用@lyz的代码在我向richtextbox添加文本时起作用。但是在从数据库检索后,超链接下划线又出现了。 - Elvin Mammadov
3个回答

31

如果有人从搜索结果到达这里,寻找简单的答案,并且不想保留任何装饰(例如使用图像时):

<Hyperlink ... TextDecorations="">
   ...
</Hyperlink>

10
我刚试过了,它有效。
Xaml
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="Root">
        <TextBlock x:Name="TextBl"></TextBlock>
    </Grid>
</Window>

代码后台

Run run = new Run("Stackoverflow");
Hyperlink hyper = new Hyperlink(run);
hyper.NavigateUri = new Uri("http://stackoverflow.com");
hyper.TextDecorations = null;
TextBl.Inlines.Add(hyper);

但是当我从数据库加载它时,下划线又出现了。 - Elvin Mammadov
你的textRange是什么?你将超链接添加到哪个元素中?如果你能展示更多的代码。 - lyz
请问您要将超链接添加到哪个元素上?我在您更新的代码中没有看到它。 - lyz
there is not other element. - Elvin Mammadov

6

从你的C#代码中删除颜色格式,并将以下内容放入你的App.xaml文件:

<Application.Resources>
    ...
    <TextDecorationCollection x:Key="_textDeco_hyperlink">
        <!--<TextDecoration Location="Underline" />-->
    </TextDecorationCollection>

    <Style TargetType="{x:Type Hyperlink}">
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Background" Value="Khaki" />
        <Setter Property="ForceCursor" Value="True" />
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="TextDecorations" Value="{DynamicResource _textDeco_hyperlink}" />
        <Setter Property="IsEnabled" Value="False" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Khaki" />
                <Setter Property="Background" Value="Black" />
            </Trigger>
        </Style.Triggers>
    </Style>
    ...
<Application.Resources>

如果这不起作用,我认为问题出在你序列化和反序列化FlowDocument的方式上。请尝试以下方法:
// Store your FlowDocument as a list of strings
List<string> blocksAsStrings = FlowDoc_store(_docSelected._Rtb.Document);
...
// Load your FlowDocument into your RichTextBox
rtb.Document = FlowDoc_load(blocksAsStrings);

/// <summary>
/// Stores a FlowDocument as a list of strings, each string represents a Block.
/// </summary>
public static List<string> FlowDoc_store(FlowDocument flowDoc)
{
    List<string> blocksAsStrings = new List<string>(flowDoc.Blocks.Count);

    foreach (Block block in flowDoc.Blocks)
    {
        blocksAsStrings.Add(XamlWriter.Save(block));
    }

    return blocksAsStrings;
}

/// <summary>
/// Loads a FlowDocument from a list of strings, each string represents a Block.
/// </summary>
public static FlowDocument FlowDoc_load(List<string> blocksAsStrings)
{
    FlowDocument flowDoc = new FlowDocument();

    foreach (string blockAsString in blocksAsStrings)
    {
        using (StringReader stringReader = new StringReader(blockAsString))
        {
            using (XmlReader xmlReader = XmlReader.Create(stringReader))
            {
                Block block = (Block)XamlReader.Load(xmlReader);
                flowDoc.Blocks.Add(block);
            }
        }
    }

    return flowDoc;
}

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