RichTextBox如何在wpf中跳转到texttrange

RichTextBox如何在wpf中跳转到texttrange,wpf,Wpf,当页面上有大量内容时,检索内容可能不在当前窗口中。我希望跳转到检索内容所在的页面。目前,检索是通过textrange完成的。我希望根据文本范围跳转到过去。我的英语不好,请帮帮我,这是我的代码 string searchStr = ""; List<TextRange> textRanges = new List<TextRange>(); int currentPoint, oldCurrentPoint; intern

当页面上有大量内容时,检索内容可能不在当前窗口中。我希望跳转到检索内容所在的页面。目前,检索是通过textrange完成的。我希望根据文本范围跳转到过去。我的英语不好,请帮帮我,这是我的代码

    string searchStr = "";
    List<TextRange> textRanges = new List<TextRange>();
    int currentPoint, oldCurrentPoint;
    internal void findContent(string text, RichTextBox richtextbox, bool diretion)
    {
        
        if (string.IsNullOrEmpty(text)) return;           
        if (textRanges.Count != 0) textRanges[oldCurrentPoint].ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Transparent);
        if (searchStr != text)
        {
            textRanges = FindTextInRange(richtextbox.Document.ContentStart, richtextbox.Document.ContentEnd, text);
            currentPoint = 0;
            oldCurrentPoint = 0;
            searchStr = text;
        }
        if (textRanges.Count == 0) return;
        oldCurrentPoint = currentPoint;
        if (diretion)
        {
            currentPoint++;
            if (currentPoint > textRanges.Count - 1) currentPoint = 0;
        }
        else
        {
            currentPoint--;
            if (currentPoint < 0) currentPoint = textRanges.Count - 1;
        }

        textRanges[oldCurrentPoint].ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Transparent);
        textRanges[currentPoint].ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Color.FromRgb(0x49, 0x3e, 0x8c)));
    }

    public List<TextRange> FindTextInRange(TextPointer contentStart, TextPointer contentEnd, string searchText)
    {
        List<TextRange> results = new List<TextRange>();
        int offset = 0;
        while (offset >= 0)
        {
            var searchRange = new TextRange(contentStart, contentEnd);
            offset = searchRange.Text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
            if (offset < 0)
                break;  // Not found

            var start = GetTextPositionAtOffset(searchRange.Start, offset);
            TextRange result = new TextRange(start, GetTextPositionAtOffset(start, searchText.Length));
            contentStart = GetTextPositionAtOffset(start, searchText.Length);
            results.Add(result);
        }
        return results;
    }

    TextPointer GetTextPositionAtOffset(TextPointer position, int characterCount)
    {
        while (position != null)
        {
            if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
            {
                int count = position.GetTextRunLength(LogicalDirection.Forward);
                if (characterCount <= count)
                {
                    return position.GetPositionAtOffset(characterCount);
                }

                characterCount -= count;
            }

            TextPointer nextContextPosition = position.GetNextContextPosition(LogicalDirection.Forward);
            if (nextContextPosition == null)
                return position;

            position = nextContextPosition;
        }

        return position;
    }
string searchStr=”“;
List textRanges=新列表();
int currentPoint,oldCurrentPoint;
内部void find内容(字符串文本、RichTextBox、RichTextBox、bool diretion)
{
if(string.IsNullOrEmpty(text))返回;
如果(textRanges.Count!=0)textRanges[oldCurrentPoint].ApplyPropertyValue(TextElement.BackgroundProperty,Bruss.Transparent);
如果(searchStr!=文本)
{
textRanges=findtexterange(richtextbox.Document.ContentStart,richtextbox.Document.ContentEnd,text);
电流点=0;
oldCurrentPoint=0;
searchStr=文本;
}
如果(textRanges.Count==0)返回;
oldCurrentPoint=当前点;
如果(方向)
{
currentPoint++;
如果(currentPoint>textRanges.Count-1)currentPoint=0;
}
其他的
{
当前点--;
如果(currentPoint<0)currentPoint=textRanges.Count-1;
}
textRanges[oldCurrentPoint].ApplyPropertyValue(TextElement.BackgroundProperty,Bruss.Transparent);
textRanges[currentPoint].ApplyPropertyValue(TextElement.BackgroundProperty,新的SolidColorBrush(Color.FromRgb(0x49,0x3e,0x8c));
}
公共列表查找范围(文本指针contentStart、文本指针contentEnd、字符串搜索文本)
{
列表结果=新列表();
整数偏移=0;
而(偏移量>=0)
{
var searchRange=新文本范围(contentStart、contentEnd);
offset=searchRange.Text.IndexOf(searchText,StringComparison.OrdinalIgnoreCase);
如果(偏移量<0)
break;//找不到
var start=GetTextPositionAtOffset(searchRange.start,offset);
TextRange结果=新的TextRange(开始,GetTextPositionAtOffset(开始,searchText.Length));
contentStart=GetTextPositionAtOffset(start,searchText.Length);
结果。添加(结果);
}
返回结果;
}
text指针GetTextPositionAtOffset(text指针位置,int字符计数)
{
while(位置!=null)
{
if(position.GetPointerContext(LogicalDirection.Forward)=TextPointerContext.Text)
{
int count=position.gettextunlength(LogicalDirection.Forward);

如果(characterCount)我找到了一种方法“Rect r=textDirection.GetCharacterRect(LogicalDirection.Backward);rtb.ScrollToVerticalOffset(r.Y);”var characterRect=textPointer.GetCharacterRect(LogicalDirection.Forward);RichTextBox.ScrollToHorizontalOffset(RichTextBox.HorizontalOffset+characterRect.Left-RichTextBox.ActualWidth/2d);RichTextBox.ScrollToVerticalOffset(RichTextBox.VerticalOffset+characterRect.Top-RichTextBox.ActualHeight/2d);