Not sure if this is what you had in mind, but there is a proof of concept for RichTextBox, which stores the cursor in the center where the user places it (clicks in the field).
Although, since Omkar said you would need to add spaces if the document was scrolled to the beginning or the end, you need to add white color to scroll through the text.
<RichTextBox HorizontalAlignment="Left" Height="311" VerticalAlignment="Top" Width="509" PreviewKeyDown="HandleKeyDownEvent"> <FlowDocument> <Paragraph Margin="0"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla turpis sem, tincidunt id vestibulum venenatis, fermentum eget orci. Donec mollis neque ac leo tincidunt tempus. Pellentesque mollis, nunc sit amet fermentum rutrum, lectus augue ultrices nibh, at lacinia est est ut justo. Cras non quam eu enim vulputate porttitor eu sit amet lectus. Suspendisse potenti. Maecenas metus nunc, dapibus id dapibus rhoncus, semper quis leo. Pellentesque eget risus magna, dignissim aliquam diam. Morbi. </Paragraph> </FlowDocument> </RichTextBox>
In the code behind:
private void HandleKeyDownEvent(object sender, KeyEventArgs e) { RichTextBox rtb = sender as RichTextBox; if (rtb != null) { //text to scroll up relative to caret if (e.Key == Key.Down) { Block paragraph; //get the whitespace paragraph at end of documnent paragraph = rtb.Document.Blocks .Where(x => x.Name == "lastParagraph") .FirstOrDefault(); // if there is no white space paragraph create it if (paragraph == null) { paragraph = new Paragraph { Name = "lastParagraph", Margin = new Thickness(0) }; //add to the end of the document rtb.Document.Blocks.InsertAfter(rtb.Document.Blocks.LastBlock, paragraph); } // if viewport larger than document, add whitespace content to fill view port if (rtb.ExtentHeight < rtb.ViewportHeight) { Thickness margin = new Thickness() { Top = rtb.ViewportHeight - rtb.ExtentHeight }; margin.Bottom = rtb.ViewportHeight - rtb.ExtentHeight; paragraph.Margin = margin; } // if the document has been scrolled to the end or doesn't fill the view port if (rtb.VerticalOffset + rtb.ViewportHeight == rtb.ExtentHeight) { // and a line to the white paragraph paragraph.ContentEnd.InsertLineBreak(); } //move the text up relative to caret rtb.LineDown(); } // text is to scroll download relative to caret if (e.Key == Key.Up) { // get whitespace at start of document Block paragraph; paragraph = rtb.Document.Blocks .Where(x => x.Name == "firstParagraph") .FirstOrDefault(); //if whitespace paragraph is null append a new one if (paragraph == null) { paragraph = new Paragraph { Name = "firstParagraph", Margin = new Thickness(0) }; rtb.Document.Blocks.InsertBefore(rtb.Document.Blocks.FirstBlock, paragraph); } // up document is at top add white space if (rtb.VerticalOffset == 0.0) { paragraph.ContentStart.InsertLineBreak(); } //move text one line down relative to caret rtb.LineUp(); } } }
EDIT: This approach seems to work. The line height is determined using the difference between the top of one line and the next, avoiding the problem of line breaking performing offset.
<RichTextBox PreviewKeyDown="PreviewKeyDownHandler"> <FlowDocument> </FlowDocument> </RichTextBox>
In the code behind:
private void PreviewKeyDownHandler(object sender, KeyEventArgs e) { RichTextBox rtb = sender as RichTextBox; if (rtb != null) { if (e.Key == Key.Down) {
Luke forder
source share