Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过WPF中的隐藏代码为每个段落设置RichTextBox的滚动位置?_C#_Wpf_Scrollbar_Richtextbox - Fatal编程技术网

C# 如何通过WPF中的隐藏代码为每个段落设置RichTextBox的滚动位置?

C# 如何通过WPF中的隐藏代码为每个段落设置RichTextBox的滚动位置?,c#,wpf,scrollbar,richtextbox,C#,Wpf,Scrollbar,Richtextbox,我有一个丰富的文本框和一个包含几个段落的长文本。我想写一些隐藏的代码来调整滚动条的垂直位置,以聚焦于特定的段落。是否可以根据富文本框的大小和段落位置计算垂直偏移 RichTextBox.ScrollToVerticalOffset(calculatedData) 为了获得位置,首先,您应该在段落开头获得插入符号的位置。然后,得到边界框的矩形。矩形的Y属性可以滚动RichTextBox第一个段落 private void RichTextBox_OnLoaded(object s

我有一个丰富的文本框和一个包含几个段落的长文本。我想写一些隐藏的代码来调整滚动条的垂直位置,以聚焦于特定的段落。是否可以根据富文本框的大小和段落位置计算垂直偏移

RichTextBox.ScrollToVerticalOffset(calculatedData)

为了获得位置,首先,您应该在段落开头获得插入符号的位置。然后,得到边界框的矩形。矩形的Y属性可以滚动RichTextBox第一个段落

        private void RichTextBox_OnLoaded(object sender, RoutedEventArgs e)
        {
            // Get the paragraph block text
            var textBlock = RichTextBox.Document.Blocks.ElementAt(2);
            //get the caret position of the start of the paragraph
            var startOfTextBlock = textBlock.ContentStart;
            // get the the character rectangle
            Rect charRect = startOfTextBlock.GetCharacterRect(LogicalDirection.Forward);
            // set the the vertical offset ot the Y position of the rectangle 
            RichTextBox.ScrollToVerticalOffset(charRect.Y);
        }