-2

In a Console application you can use "Console.WriteLine" to log an event, in WPF you can do the same using a RichTextBox code behind something like this :

public Logger(RichTextBox loggingRichTextBox)
{
    _content = new FlowDocument();

    LoggingRichTextBox = loggingRichTextBox;
    LoggingRichTextBox.Document = _content;
}

I can't figure out how to do this with MVVM as you can't pass the RichTextBox control to the ViewModel?

Anyone has a example? Is it even possible or is there a better way?

  • 1
    Ray Burns gave a good answer on this at this SO post: (https://stackoverflow.com/questions/343468/richtextbox-wpf-binding) – Ryan Wilson Jan 07 '21 at 15:36
  • 1. Use a Logging Framework. 2. Use a Logging Framework that supports a sink/target of WPF Control. Some examples: https://github.com/umairsyed613/Serilog.Sinks.WPF, https://stackoverflow.com/q/6617689/982149 , ... – Fildor Jan 07 '21 at 15:55
  • I'm going to use a ListBox – user11208424 Jan 07 '21 at 16:33

1 Answers1

0

For debugging purposes, you can use System.Diagnostics.Debug.WriteLine(), which will log on the attached debugger output screen.

See: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.writeline?view=net-5.0

But if you want to display the output for the user then it is possible to bind a TextBlock Text property to the ViewModel and append logged lines to it.

Ahmed Zaki
  • 104
  • 3
  • 11