目標是向RichTextBox添加一列段落號(數字顯示該richtextbox.Document.Blocks中段落的索引)。目前,我使用的代碼塊中的RichTextBox的LayoutUpdated事件:什麼是創建編號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();
...我就說說我在這裏加入...
scv.Content = grid;
UpdateLayout();
}
}
}
在電網,我添加了兩列,第一個是StackPanel,第二個是文檔查看。對於每個段落,我將一個TextBlock添加到StackPanel.Children,並使用Paragraph.ElementStart.GetCharacterRect(LogicalDirection.Forward)方法和返回的Rect(s)的頂部屬性設置每個textBlock的高度。
一切都很好,當小於500段時,編號更新很快,但隨着文本變大,變慢。我怎樣才能讓它更有效率?我應該使用Canvas而不是StackPanel嗎?或者有沒有更好的方法來做到這一點?
謝謝。
感謝您的回答。我會去嘗試一下。但我不知道是否使用GetCharacterRect(...)方法是獲取段落高度的好方法。我的意思是還有更好的辦法嗎? – Ramin
我不熟悉GetCharacterRect – Paparazzi