Add clickable hyperlinks to a RichTextBox without new paragraph

后端 未结 4 772
情深已故
情深已故 2020-12-06 15:49

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

I want something

相关标签:
4条回答
  • 2020-12-06 15:58

    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.");
        } 
    

    enter image description here

    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?

    0 讨论(0)
  • 2020-12-06 15:58

    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>
    
    0 讨论(0)
  • 2020-12-06 16:03

    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);
    
    0 讨论(0)
  • 2020-12-06 16:18

    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

    0 讨论(0)
提交回复
热议问题