Is there a WPF output control monitor such as Visual Studio debug output

I am trying to add an output monitor to my WPF application. A read-only monitor similar to the debug output in visual studio.

Is there a WPF control that already provides the functions I need? Or is there a way I could reuse a control from Visual Studio?

I am currently using the standard TextBox supported by StringBuilder. Updates go to StringBuilder, while TextBox receives the newest line every 200 ms.

My problem is that it becomes very slow as the output length is longer.

+4
source share
1 answer

I would use a RichTextBox control to output data.

In this example, I had no performance issues at all.

public partial class MainWindow : Window { private int counter = 0; public MainWindow() { InitializeComponent(); Loaded+=OnLoaded; } private void OnLoaded(object sender, RoutedEventArgs routedEventArgs) { for (int i = 0; i < 200; i++) { AddLine(counter++ + ": Initial data"); } var timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 0, 0, 200); timer.Tick += TimerOnTick; timer.IsEnabled = true; } private void TimerOnTick(object sender, EventArgs eventArgs) { AddLine(counter++ + ": Random text"); } public void AddLine(string text) { outputBox.AppendText(text); outputBox.AppendText("\u2028"); // Linebreak, not paragraph break outputBox.ScrollToEnd(); } } 

And xaml

 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <RichTextBox x:Name="outputBox" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" IsReadOnly="True"> <FlowDocument/> </RichTextBox> </Grid> </Window> 

And it is probably easy to extend. If the scroll position is not at the end, do not scroll to the end, for example, to view old data while the text field is still updating.

+2
source

All Articles