/// <summary> /// If scroll viewer is already scrolled all the way down, scroll to /// bottom after printing new line of content. /// Otherwise, if scroll viewer is scrolled up more than 1 pixel above /// the bottom of the scrollable content, do not scroll. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { var offset = OutputScrollViewer.VerticalOffset; if (offset == (OutputScrollViewer.ExtentHeight - OutputScrollViewer.ViewportHeight)) { OutputScrollViewer.ScrollToBottom(); } }
/// <summary> /// If scroll viewer is already scrolled all the way down, scroll to /// bottom after printing new line of content. /// Otherwise, if scroll viewer is scrolled up more than 1 pixel above /// the bottom of the scrollable content, do not scroll. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { bool scrollBarAtBottom = OutputScrollViewer.VerticalOffset == (OutputScrollViewer.ExtentHeight - OutputScrollViewer.ViewportHeight); if (scrollBarAtBottom) { OutputScrollViewer.ScrollToBottom(); } }
private void OutputStatusMessage(String msg) { var splits = msg.Split('\n'); foreach (var split in splits) { var trimmed = split.Trim('\r'); // Remember that SendStatusMessage is called from ReceiveMessage, which is assigned to a delegate of the example base class. // Since the example base class is executed outside of the main thread i.e. RunAsync, you must check to make sure this thread // has access to components of the application window i.e. OutputScrollViewer. if (!Dispatcher.CheckAccess()) { Dispatcher.Invoke(new SendStatusMessageDelegate(OutputStatusMessage), msg); return; } OutputScrollViewer.Content += (trimmed + "\r\n"); OutputScrollViewer.ScrollToBottom(); } }
public void WriteData(string data) { //Paragraph paragraph = new Paragraph(); //paragraph.Inlines.Add(data); //OutputRichTextBox.Document.Blocks.Add(paragraph); data = DateTime.Now.ToString("HH:mm:ss.ffffff") + data; Debug.WriteLine($"DATA:[{data}]"); Dispatcher.InvokeAsync(() => { Paragraph paragraph = new Paragraph(); // Parse color code Brush currentColor = Brushes.LightGray; string remaining = data; while (true) { int startIndex = remaining.IndexOf("%", StringComparison.OrdinalIgnoreCase); //Debug.WriteLine($"StartIndex:[{startIndex}]"); if (startIndex >= 0) { string preceding = remaining.Substring(0, startIndex); remaining = remaining.Substring(startIndex + 1); //Debug.WriteLine($"Preceding:[{preceding}]"); //Debug.WriteLine($"Remaining:[{remaining}]"); AddColoredTextToParagraph(paragraph, currentColor, preceding); //Debug.WriteLine($"AddColoredTextToParagraph preceding:[{preceding}] {currentColor}"); int endIndex = remaining.IndexOf("%", StringComparison.OrdinalIgnoreCase); //Debug.WriteLine($"EndIndex:[{endIndex}]"); if (endIndex == 1) // %c% { string colorCode = remaining.Substring(0, endIndex); //Debug.WriteLine($"ColorCode:[{colorCode}]"); Brush newColor = GetColor(colorCode); if (newColor != null) { currentColor = newColor; remaining = remaining.Substring(endIndex + 1); } else { //Debug.WriteLine($"AddColoredTextToParagraph % not color:[%] {currentColor}"); AddColoredTextToParagraph(paragraph, currentColor, "%"); } } else { //Debug.WriteLine($"AddColoredTextToParagraph %:[%] {currentColor}"); AddColoredTextToParagraph(paragraph, currentColor, "%"); } } else { //Debug.WriteLine($"AddColoredTextToParagraph remaining:[{remaining}] {currentColor}"); AddColoredTextToParagraph(paragraph, currentColor, remaining); break; } } OutputRichTextBox.Document.Blocks.Add(paragraph); OutputScrollViewer.ScrollToBottom(); }); }
private void ToggleButton_Click(object sender, RoutedEventArgs e) { OutputScrollViewer.ScrollToBottom(); }