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);
}