Rich Text Box/Flow Document > Formatting (list order, line spacing & bullet points)

StackOverflow https://stackoverflow.com/questions/22371744

  •  14-06-2023
  •  | 
  •  

سؤال

I have a RichTextBox in a WPF application that serves as an output container for status updates. Each line added is colored based on it's informational level (warning-yellow, info-gray and so on).

Paragraph currentStatus = new Paragraph(new Run("ERROR: Couldn't find stuffs."));
currentStatus.Foreground = System.Windows.Media.Brushes.Red;
List myList = new List();
myList.ListItems.Add(new ListItem(currentStatus));
rtbStatus.Document.Blocks.Add(myList);  // existing rich textbox

Though it is technically working, after hours of digging I still have a few formatting problems I can't seem to over-come or research out:

  1. I want the list to be inverted, with the most recent 'post' at the top. I have been able to achieve this a couple of different ways, but each at a cost of losing the previous color formatting to the default foreground color of the control (the app has a visual buffer of about 10 lines when spacing is ideal that needs to retain the color applied).

  2. I want the line spacing to be normal, w/o padding between lines. There is enough room for almost 2 more lines between each 'post' when using a list and I am looking for something resembling a textblock's multi-line spacing (see screen linkage below).

  3. I'd love to get rid of the bullet points if a list is the way to go.

A couple notes: This all has to be done on the back-end, and I would like to look at a smooth auto-scrolling animation as a future feature release, though I haven't researched it yet (off-thread topic).

Now, everything I am reading leads me to believe a richTextBox>flowDocument>list is my best solution as I couldn't figure out how leverage the AppendText() method with a line break (environment.NewLine works, but has an even greater amount of padding between lines) nor work out the color dynamics when using other controls, but I am a novice in the C# world.

Please tell me if I am doing this the hard way first and foremost. But if anyone has ideas on how to achieve the above it'd be greatly appreciated.

Image of the above syntax:

enter image description here

Image of the desired spacing results using textblock:

Thanks in advance.

هل كانت مفيدة؟

المحلول

I found some properties that allowed me to accomplish this, so I removed the list and after some tweaking came up with the below:

 // Status Update
        public void UpdateStatus(string eventLevel, string message)
        {
            Paragraph currentStatus = new Paragraph(new Run(message));
            if (eventLevel == "info")
                currentStatus.Foreground = System.Windows.Media.Brushes.Gray;

            if (eventLevel == "warning")
                currentStatus.Foreground = System.Windows.Media.Brushes.Yellow;

            if (eventLevel == "error")
                currentStatus.Foreground = System.Windows.Media.Brushes.Red;

            if (eventLevel == "highlight")
                currentStatus.Foreground = System.Windows.Media.Brushes.CornflowerBlue;

            currentStatus.LineHeight = 1;
            rtbStatus.Document.Blocks.InsertBefore(rtbStatus.Document.Blocks.FirstBlock, currentStatus);
        }

and can now append colored lines to the 'top' with a minimal line space:

UpdateStatus("error", "My custom error message");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top