简体   繁体   中英

Add clickable hyperlinks to a RichTextBox without new paragraph

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.

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?

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);

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM