Add clickable hyperlinks to a RichTextBox without new paragraph

☆樱花仙子☆ 提交于 2019-11-27 06:31:11

问题


Is it possible to dynamically add hyperlinks without creating new paragraphs like in this question Dynamically adding hyperlinks to a RichTextBox?

I want something like "Please visit http://www.google.com. Thank you!" not

"Please visit

http://www.google.com

.Thank you!".

Also RichTextBox must be readonly, user cannot type in it. It's something like log, all I need is to periodically add some text which sometimes contains URLs.


回答1:


OK, looks like here is what I need (thanks @Blam and @PaulN Dynamically adding hyperlinks to a RichTextBox):

    public MainWindow()
    {
        InitializeComponent();

        rtb.IsDocumentEnabled = true;
        rtb.Document.Blocks.FirstBlock.Margin = new Thickness(0);
    }

    private void AddHyperlinkText(string linkURL, string linkName, 
              string TextBeforeLink, string TextAfterLink)
    {
        Paragraph para = new Paragraph();
        para.Margin = new Thickness(0); // remove indent between paragraphs

        Hyperlink link = new Hyperlink();
        link.IsEnabled = true;
        link.Inlines.Add(linkName);
        link.NavigateUri = new Uri(linkURL);
        link.RequestNavigate += (sender, args) => Process.Start(args.Uri.ToString()); 

        para.Inlines.Add(new Run("[" + DateTime.Now.ToLongTimeString() + "]: "));
        para.Inlines.Add(TextBeforeLink);
        para.Inlines.Add(link);
        para.Inlines.Add(new Run(TextAfterLink)); 

        rtb.Document.Blocks.Add(para);
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {   
        AddHyperlinkText("http://www.google.com", "http://www.google.com", 
               "Please visit ", ". Thank you! Some veeeeeeeeeery looooooong text.");
    } 

But one little problem left: maybe someone know how to remove blank space at the beginning which is marked with the red line on the image above?




回答2:


As for making a RichTextBox or TextBox read only

TextBoxBase.IsReadOnly Property

For adding text you can use a run

    FlowDocument doc = new FlowDocument();
    rtb.Document = doc;
    rtb.IsReadOnly = true;

    Paragraph para = new Paragraph();
    doc.Blocks.Add(para);

    Hyperlink link = new Hyperlink();
    link.IsEnabled = true;
    link.Inlines.Add("Hyperlink");
    link.NavigateUri = new Uri("http://www.google.co.uk");
    para.Inlines.Add(link);
    Run run = new Run();
    run.Text = " next words";
    para.Inlines.Add(run);



回答3:


You can do it with

  <ContentControl>
    <Span>
        <Run Text="Please visit"/>
        <Hyperlink NavigateUri="http://google.com">
            <Run Text="google"/>
        </Hyperlink>
        <Run Text=". Thank you!"/>
    </Span>
</ContentControl>

And if you are in a navigationFrame you get the hyperlink functionality for free

Or...

<StackPanel Orientation="Horizontal">
<TextBlock Text="Please visit"/>
<Button Style="linkButton" Content="Google" Command/Click="GotoGoogle"/>
<TextBlock Text=". Thank you!"/>
</StackPanel>



回答4:


Note: To Remove the Blank Line from the RichText by doing the following:

MyRichTextBox.Document.Blocks.Clear();

move blank space at the beginning of RichTextBox as you add Paragraph Runs



来源:https://stackoverflow.com/questions/12303414/add-clickable-hyperlinks-to-a-richtextbox-without-new-paragraph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!