[英]What is the best way to create a Numbered RichtextBox?
目标是将一列段落编号添加到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();
}
}
}
在网格中 ,我添加了两列,第一列是StackPanel,第二列是documentView 。 对于每个段落,我将一个TextBlock添加到StackPanel.Children中,并通过使用Paragraph.ElementStart.GetCharacterRect(LogicalDirection.Forward)方法和返回的Rect的顶部和底部属性来设置每个textBlock的高度。
一切都很好,并且当段落少于500个时,编号会快速更新,但是随着文本变大,速度会变慢。 如何提高效率? 我应该使用Canvas而不是StackPanel吗? 还是有更好的方法呢?
谢谢。
ListView GridView。 支持虚拟化。 我在一些非常大的文档中为每一行使用textblock,效果很好。
我使用了我在问题中提到的过程,然后使用了Dispacher.BeginInvoke(...)
方法。 我将DispatcherPriority
设置为ApplicationIdle
。 当添加宽度chaneges或添加新Paragraph
时,我将其称为。 像这样:
_updateIfWidthChangedDispacherO= _ownerTextBox.Dispatcher.BeginInvoke((Action)(() =>
{
updateIfWidthChanged();
}),
DispatcherPriority.ApplicationIdle);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.