【问题标题】:Remove underline of dynamic hyperlink in WPF删除 WPF 中动态超链接的下划线
【发布时间】:2016-04-30 04:44:37
【问题描述】:

我创建 WPF 应用程序。在某种形式中,用户将 richtextbox 的选定文本更改为 hyperlink。我搜索了一个多小时并寻找解决方案。但不能。 我的动态超链接创建如下:

                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,但是代码不行。

【问题讨论】:

  • 很抱歉,有一点我不明白:你必须保留任何其他 TextDecoration 吗?或者您可以将它们全部删除吗?
  • @Elvin Mammadov:您的应用程序中的所有超链接都应该具有相同的格式吗?
  • 是的。我将这些超链接添加到数据库,当我将文本添加到富文本框时,@lyz 的代码可以工作。但是从数据库超链接检索后又来了下划线

标签: c# wpf hyperlink richtextbox text-decorations


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我刚刚尝试过,它成功了
    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);
    

    【讨论】:

    • 但是当我从数据库中加载它时,下划线又出现了
    • 您的 textRange 是什么,您将超链接添加到哪个元素?如果您可以显示更多代码
    • 请问您将超链接添加到哪个元素?我在您更新的代码中没有看到它
    • 没有其他元素。
    【解决方案3】:

    从您的 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-06
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 2013-09-19
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多