简体   繁体   中英

What is the best way to create a Numbered RichtextBox?

The goal is to add a column of paragraph numbers to a RichTextBox (the numbers show the index of paragraphs in that richtextbox.Document.Blocks). Currently, I use this block of code in LayoutUpdated event of the RichTextBox:

bool _added=false
void onLayoutUpdated(object sender, EventArgs e)
{
   if (!_added)
   {
      _added= true;
      scv = Helper.GetFind.FindChild<ScrollViewer>(this, null);
      if (scv != null)
      {
         FrameworkElement  documentView = scv.Content as FrameworkElement;
         scv.ClearValue(ScrollViewer.ContentProperty);
         Grid grid= new Grid();

... I will talk about what I have added here...

         scv.Content = grid;
         UpdateLayout();
      }
   }
}

In the grid , I add two columns, the first one is a StackPanel and the second one is the documentView . For each paragraph I add a TextBlock to the StackPanel.Children and I set the height of each textBlock by using Paragraph.ElementStart.GetCharacterRect(LogicalDirection.Forward) methods and the Top & Bottom Properties of the returned Rect (s).

Everything is fine and when there are less than 500 paragraphs, the numbering updates quickly, But as the text gets larger, it gets slower. How can I make it more efficient? Should I use a Canvas instead of a StackPanel? Or is there any better way of doing this?

Thanks.

ListView GridView. Support virtualiztion. I use textblock for each line in some very large document and works great.

I used the procedure I mensioned in the question and then Dispacher.BeginInvoke(...) method. I set DispatcherPriority to ApplicationIdle . I call it when Width chaneges or new Paragraph is added. Sth like this:

 _updateIfWidthChangedDispacherO= _ownerTextBox.Dispatcher.BeginInvoke((Action)(() =>
        {
            updateIfWidthChanged();
        }),
        DispatcherPriority.ApplicationIdle);

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