I'm trying to get the lines of the RichTextBox
.
Here they show how to do it:
Using GetLineStartPosition to get the end of a line in WPF RichTextBox
But for whatever reason I always get null
as the return of GetLineStartPosition(1)
.
XAML
<Grid>
<StackPanel>
<Button Height="40" Click="Button_Click"></Button>
<RichTextBox x:Name="rtbEditor">
<FlowDocument>
<Paragraph>Hello, world!
a
<LineBreak/>
b
<LineBreak/>
c
<LineBreak/>
d
</Paragraph>
</FlowDocument>
</RichTextBox>
</StackPanel>
</Grid>
Code
public MainWindow()
{
InitializeComponent();
rtbEditor.AppendText("testtext" + "\r");
rtbEditor.AppendText("testtext" + "\r");
rtbEditor.AppendText("testtext" + "\r");
rtbEditor.AppendText("testtext" + "\r\n");
rtbEditor.AppendText("testtext" + "\r\n");
rtbEditor.AppendText("testtext" + "\r\n");
rtbEditor.AppendText("testtext" + "\n");
rtbEditor.AppendText("testtext" + "\n");
rtbEditor.AppendText("testtext" + "\n");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TextPointer contentStart = rtbEditor.Document.ContentStart;
var nextStart = contentStart.GetLineStartPosition(1);
}
nextStart
is null
. What am I doing wrong here?