{"id":2703,"date":"2019-10-24T13:19:27","date_gmt":"2019-10-24T12:19:27","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2703"},"modified":"2022-07-25T08:33:25","modified_gmt":"2022-07-25T07:33:25","slug":"c-wpf-richtextbox","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-wpf-richtextbox\/","title":{"rendered":"C# WPF RichTextBox"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">private void AppendText(RichTextBox box, string text, LogLevel level)\n{\n\tColor color = Color.FromRgb(0, 0, 0);\n\tswitch (level)\n\t{\n\t\tcase LogLevel.Debug: color = Color.FromRgb(200, 200, 200); break;\n\t\tcase LogLevel.Info: color = Color.FromRgb(100, 100, 200); break;\n\t\tcase LogLevel.Warning: color = Color.FromRgb(200, 200, 0); break;\n\t\tcase LogLevel.Error: color = Color.FromRgb(200, 0, 0); break;\n\t}\n\n\tBrushConverter bc = new BrushConverter();\n\tParagraph paragraph = new Paragraph();\n\tparagraph.Foreground = (Brush)bc.ConvertFromString(HexConverter(color));\n\tparagraph.Inlines.Add(text);\n\tbox.Document.Blocks.Add(paragraph);\n\t\/\/ Slower, but also works:\n\t\/\/TextRange tr = new TextRange(box.Document.ContentEnd, box.Document.ContentEnd);\n\t\/\/tr.Text = text + \"\\r\\n\";\n\t\/\/try\n\t\/\/{\n\t\/\/    tr.ApplyPropertyValue(TextElement.ForegroundProperty, bc.ConvertFromString(HexConverter(color)));\n\t\/\/}\n\t\/\/catch (FormatException) { }\n\tbox.ScrollToEnd();\n}\nprivate void ClearText(RichTextBox box) {\n\tbox.Document.Blocks.Clear();\n}\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-2703","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2703"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2703\/revisions"}],"predecessor-version":[{"id":2704,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2703\/revisions\/2704"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}