Task: display the message log in the textbox during the operation of the application. Please tell me how best to implement this using MVVM.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Zabindit can be.

Xaml:

<RichTextBox Width="200" Height="100"> <FlowDocument> <Paragraph> <Run Text="{Binding Path=LineFormatted}" /> </Paragraph> </FlowDocument> </RichTextBox> 

View model:

 private string lineFormatted; public string LineFormatted { get { return this.lineFormatted; } set { this.SetProperty(ref this.lineFormatted, value); } } public void WriteTextLine(string val) { this.LineFormatted += $"[{DateTime.Now.ToLongTimeString()}] {val}\r\n"; } 

Where LineFormatted uses INotifyPropertyChanged as it is implemented in you (for me it is SetProperty ).

Test:

 WriteTextLine("testing line");