wpf richtextbox BringIntoView无法使用TextRange.ClearAllProperties?

时间:2015-12-10 14:19:16

标签: c# wpf scroll richtextbox

我正在编程wpf.i当突出显示的单词从viewportHeight移动时必须向上滚动一页。所以我使用下面的代码。它工作正常。

FrameworkContentElement fce = (textRange.Start.Parent as FrameworkContentElement);
            if (fce != null)
            {
                fce.BringIntoView();
            }

但是我需要使用下面的代码来突出显示单词。

   TextRange fullRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
                fullRange.ClearAllProperties(); 
                TextPointer start = fullRange.Start.GetPositionAtOffset(offset);
                TextPointer end = start.GetPositionAtOffset(length);
                TextRange textRange = rtb.Selection;
                textRange.Select(start, end);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(m_DehighlightbackgroundColor));
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(m_DehighlightforegroundColor));

我使用fullRange.ClearAllProperties(); fce.BringIntoView();后无效。我的意思是不滚动到突出显示的字词。

那么,如何解决这个问题?

此致 阿琼

1 个答案:

答案 0 :(得分:0)

这个答案为我解决了类似的问题:

How to bring Inline from a RichTextBox Child into View 的RichTextBox - 子 - 进入 - 视图

总结一下,尝试在BringIntoView之前添加以下内容:

Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(delegate { }));

相关问题