开发者

Silverlight scrolling in RichTextBox

开发者 https://www.devze.com 2023-04-11 18:05 出处:网络
Is there a way t开发者_如何学Pythono scroll, for example, to the start of a RichTextBox from C# code?

Is there a way t开发者_如何学Pythono scroll, for example, to the start of a RichTextBox from C# code? I'm filling its content programmatically (with paragraphs containing images and run texts) and when the filling completes the location of the scroll is at the bottom of RichTextBox. I would like to set it to top, instead of bottom.

I have tried to created a workaround with wrapping my RichTextbox with a ScrollViewer: However when I call its ScrollToVerticalOffset on the ScrollViewer nothing happens, the UI doesn't get updated.


try rtb.Selection.Select(rtb.ContentStart, rtb.ContentStart); for scroll to start

and rtb.Selection.Select(rtb.ContentEnd, rtb.ContentEnd); for scroll to end


You need to reach inside the RichTextBox control to get hold of its internal ScrollViewer then call ScrollToVerticalOffset. This is done with the help of VisualTreeHelper, see this blog for the small source code of a couple of useful extension methods. With the VisualTreeEnumeration class available you can use the following code to get the ScrollViewer

 ScrollViewer sv = myRichTextBox.Descendents().OfType<ScrollViewer>().FirstOrDefault();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号