[英]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.