I have a TextBox which is bound to a string in my ViewModel.
XAML
<TextBox x:Name="TracesTextBox" IsReadOnly="True" Text="{Binding Traces, Mode=OneWay}" UndoLimit="0" IsUndoEnabled="False" Margin="5"
VerticalScrollBarVisibility="Auto" FontSize="10" TextChanged="OnTextBoxTextChanged" HorizontalScrollBarVisibility="Auto"
BorderBrush="{DynamicResource AccentColorBrush}"/>
Also I do something in the code behind to scroll the Text Box down as and when the string gets updated so that I can see the last string appended
Code Behind
private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
TracesTextBox.CaretIndex = TracesTextBox.Text.Length;
TracesTextBox.ScrollToEnd();
}
This piece of code gets executed by a Background thread which fetches data from Ethernet
ViewModel Code Invoked by Background thread
var str =((float)(msg.ArrivalTimeMs / 1000.0)).ToString("00000.000").PadRight(16, ' ') ;
str+= msg.Bytes.Take(msg.len).Aggregate(str, (current, dataByte) => current + (dataByte.ToString("X").PadLeft(2, '0') + " ")) + "\n";
UiTaskFactory.StartNew(() =>
{
Traces += str;
}
);
Problem is that after several hours of running the application the amount of text in the Traces gets very big and the UI does not respond to the scroll at all.
Now, how do I perform some kind of stripping at the beginning of the string and keep the last thousand lines that I received? I suppose this is gonna cost in terms of Processor usage. If I received fewer messages from Ethernet the scrolling is very responsive. I thought of using Queues of strings but isn't there a more elegant solution of working with several thousands of lines of string?