2009-10-23 61 views
1

我正在使用基於RichEditBox的文本編輯器。我已經實現了「Go to line」功能,最終解決爲 TextPointer.Paragraph.BringIntoView();Paragraph.BringIntoView()僅適用於聚焦時

除此之外,我還設置了插入位置。 我發現,BringIntoView只適用於我先點擊RichEditBox(專注它)。否則,它似乎被忽略。不過,我可以看到插入位置已經被BringIntoView的代碼調整了。

有人知道這個問題的原因是什麼?我如何克服它?

+0

你在說什麼langage/SDK? – 2009-10-23 16:15:53

+0

對不起,我的問題很深,我忘了細節... 該項目是在C#和WPF。我正在使用.NET 3.5 任何想法,將不勝感激。 – pavele 2009-10-26 07:40:49

+0

我也遇到過這種情況,我想這與[另一個問題]有很大關係(http://social.msdn.microsoft.com/Forums/vstudio/en-US/d9fdf56c-a651-4c9b-b0c6- 9a74eca3840d/how-to-set-read-only-property-selection-on-a-flowdocumentscrollviewer?forum = wpf#2960f16a-c66d-4ef7-b91a-eadb655a3762)在用戶點擊窗口之前無法設置選擇。微軟沒有解決這些問題令人非常沮喪。感覺像WPF被少數人所使用。 – 2013-10-12 21:53:31

回答

0

難道你不能只給RichTextBox(?)的焦點,然後使用Keyboard.Focus(richTextBox)richTextBox.Focus()

+0

當然,我先試過。它沒有幫助。但即使這樣做 - 在應用程序UI上下文中可能不太正確(更改焦點)。 – pavele 2009-10-26 11:42:58

1

找到了解決方法,不知道它是否會在純WPF環境中工作,在我的情況下,我在需要的地方使用WPF UserControls主要在Windows窗體解決方案中運行WPF。

不是立即調用BringIntoFocus(),而是將它添加到由定時器處理的隊列中,而不是立即調用它。例如:

System.Windows.Forms.Timer DeferredActionTimer = new System.Windows.Forms.Timer() { Interval = 200 }; 

Queue<Action> DeferredActions = new Queue<Action>(); 

void DeferredActionTimer_Tick(object sender, EventArgs e) { 
    while(DeferredActions.Count > 0) { 
    Action act = DeferredActions.Dequeue(); 
    act(); 
    } 
} 

在您的形式構造,或在onload事件中添加:

DeferredActionTimer.Tick += new EventHandler(DeferredActionTimer_Tick); 
DeferredActionTimer.Enabled = true; 

最後,而不是直接調用TextPointer.Paragraph.BringIntoView();,這樣稱呼它:

DeferredActions.Enqueue(() => TextPointer.Paragraph.BringIntoView()); 

注Windows窗體計時器在主線程中關閉事件(通過消息泵循環)。如果你不得不使用另一個定時器,你需要一些額外的代碼。我建議你使用System.Timers.Timer而不是System.Threading.Timer(這是多線程安全)。您還必須將該操作包裝在Dispatcher.Invoke結構中。在我的情況下,WinForms計時器就像一個魅力。