2012-11-11 50 views
0

目標是向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嗎?或者有沒有更好的方法來做到這一點?

謝謝。

回答

0

我用了我在問題中提出的程序,然後用Dispacher.BeginInvoke(...)方法。我將DispatcherPriority設置爲ApplicationIdle。我在寬度chaneges或新增Paragraph時調用它。這樣的:

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

ListView GridView。支持虛擬化。我在一些非常大的文檔中爲每一行使用textblock,並且效果很好。

+0

感謝您的回答。我會去嘗試一下。但我不知道是否使用GetCharacterRect(...)方法是獲取段落高度的好方法。我的意思是還有更好的辦法嗎? – Ramin

+0

我不熟悉GetCharacterRect – Paparazzi

相關問題