Description
1. Background code add test data
2. Using the Richtextbox.scrolltoverticaloffset () method, scroll the position of the scroll bar vertically
3. Use the timer DispatcherTimer to modify the page display data
4. Calculate your own processing, the height of the position has been scrolled
XAML Code:
<Grid> <grid.columndefinitions> <ColumnDefinitionWidth= "205*"/> <ColumnDefinitionWidth= "87*"/> </grid.columndefinitions> <Buttonx:name= "button"Content= "Start Playback"HorizontalAlignment= "Left"Margin= "2,36,0,0"VerticalAlignment= "Top"Width= " the"Grid.column= "1"Height= "$"Click= "Button_Click"/> <RichTextBoxx:name= "RichTextBox"HorizontalAlignment= "Left"Height= "209"Margin= "10,36,0,0"VerticalAlignment= "Top"Width= "The "> </RichTextBox></Grid>
Add test data code in the background:
PublicText4 () {InitializeComponent (); Richtextbox.document=Doc; Richtextbox.fontsize= -; //Add ContentAppendline (NULL,"from your world passing by"); Appendline (" One","The Moon shines on the sea"); Appendline (NULL,"from your world passing by"); Appendline (NULL,"Tianya at this time"); Appendline (" Both","Zhang Sanfeng"); Appendline (NULL,"from your world passing by"); Appendline (NULL,"Mr. Lu Xun"); Appendline (NULL,NULL);} FlowDocument Doc=NewFlowDocument ();Private voidAppendline (stringNamestringLine ) {Paragraph P=NewParagraph (); if(string. IsNullOrEmpty (name) = =false) Doc. Registername (name, p); Run R=NewRun (line); P.textalignment=Textalignment.center; P.inlines.add (R); Doc. Blocks.add (P);}
Timer Display control Code:
intPindex =0;DoubleCurtop =0;Private voidButton_Click (Objectsender, RoutedEventArgs e) { //Timing Control content display and scroll bar positionDispatcherTimer _timer =NewDispatcherTimer (); _timer. Interval= Timespan.fromseconds (1); _timer. Tick+ = (st, ET) = { //gets the contents of the specified rowBlockcollection col =richTextBox.Document.Blocks; intindex =0; TextElement prev=NULL; foreach(TextElement IteminchCol) { //Modify the style of the current row if(Index = =pindex) {Alterstyle (item, prev); } Index++; Prev=item; } pindex++; }; _timer. Start ();}Private voidAlterstyle (textelement item, TextElement prev) {//Current LineParagraph CP = Item asParagraph; Cp.foreground=brushes.red; TextRange Range=NewTextRange (Cp.contentstart, cp.contentend); //Scroll Position Control if(Pindex >0&& range. Text.length >0) { //previous line, style callback if(Prev! =NULL) {prev. Foreground=Brushes.black; } curtop+ = range. Text.length >7? +: -; Curtop+= -; Richtextbox.scrolltoverticaloffset (Curtop); }}
Operation Result:
WPF RichTextBox scroll bar auto-scroll instance, text auto-scroll instance