How to effectively transfer text to screen in WPF?

I want to pass a bunch of text to display the status / progress of a long-running task (for example, an output window in Visual Studio).

I currently have something like this XAML:

    <ScrollViewer Canvas.Left="12" Canvas.Top="12" Height="129" Name="scrollViewer1" Width="678">
        <TextBlock Name="text"  TextWrapping="Wrap"></TextBlock>
    </ScrollViewer>

and this code is behind:

    private void Update(string content)
    {
        text.Text += content + "\n";
        scrollViewer1.ScrollToBottom();
    }

After a while, he becomes very slow.
Is there a recommended way to do such things? Am I using the right kind of control?

Thank!

+3
source share
1 answer

At a minimum, you want to use readonly TextBoxand use the method AppendText()to add text.

Of course, you are still not protected from performance issues if you have enough volumes of text. In this case, you may need to study a virtualization solution (both data and UI).

+4
source

Source: https://habr.com/ru/post/1784128/


All Articles