【问题标题】:What is the best way to create a Numbered RichtextBox?创建编号 RichtextBox 的最佳方法是什么?
【发布时间】:2012-11-11 18:53:45
【问题描述】:

目标是将一列段落编号添加到 RichTextBox(数字显示该richtextbox.Document.Blocks 中的段落索引)。目前,我在 RichTextBox 的 LayoutUpdated 事件中使用了这段代码:

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

...我会说一下我在这里添加的内容...

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

grid中,我添加了两列,第一列是StackPanel,第二列是documentView。对于每个段落,我将 TextBlock 添加到 StackPanel.Children,并使用 Paragraph.ElementStart.GetCharacterRect(LogicalDirection.Forward) 方法和返回的 Rect 的顶部和底部属性设置每个文本块的高度(s)。

一切都很好,当段落少于 500 时,编号会快速更新,但随着文本变大,它会变慢。我怎样才能使它更有效率?我应该使用 Canvas 而不是 StackPanel 吗?或者有什么更好的方法吗?

谢谢。

【问题讨论】:

    标签: c# wpf richtextbox


    【解决方案1】:

    ListView 网格视图。支持虚拟化。我对一些非常大的文档中的每一行都使用了文本块,效果很好。

    【讨论】:

    • 感谢您的回答。我要试试。但我不知道使用 GetCharacterRect(...) 方法是否是获取段落高度的好方法。我的意思是有没有更好的方法?
    • GetCharacterRect我不熟悉
    【解决方案2】:

    我使用了我在问题中提到的过程,然后使用了Dispacher.BeginInvoke(...) 方法。我将DispatcherPriority 设置为ApplicationIdle。当 Width 更改或添加新的 Paragraph 时,我调用它。像这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-06
      • 2022-01-05
      • 2017-04-13
      • 1970-01-01
      • 2014-12-10
      • 2020-03-03
      • 2016-04-13
      • 2012-08-15
      相关资源
      最近更新 更多