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

Elv*_*dov 5 c# wpf richtextbox hyperlink text-decorations

我创建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;
Run Code Online (Sandbox Code Playgroud)

如何删除动态超链接的下划线。我想使用textdecoration,但是不能通过代码来实现。

lyz*_*lyz 9

我刚试过这个,它工作
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 Code Online (Sandbox Code Playgroud)

背后的代码

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


Chr*_*ris 6

对于从搜索结果到达此处的任何人,他们都在寻找简单的答案,并且不希望保留任何装饰(例如,使用图像时):

<Hyperlink ... TextDecorations="">
    ...
</Hyperlink>
Run Code Online (Sandbox Code Playgroud)


Pol*_*zer 5

从您的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>
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,我认为这是您序列化和反序列化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;
}
Run Code Online (Sandbox Code Playgroud)