title: Procedurally generating text with inline <span> XAML from a binding
body: <p>I am building a C# WPF application using MVVM to decouple the view from the business logic.</p>
  
  <p>One of my data sources is a legacy application that outputs ANSI color codes and I would like to faithfully reproduce these in the UI using textblocks, or boxes, or whatever is most appropriate.</p>
  
  <p>I have been able to write a very simple converter to turn the ANSI codes into  elements with styles to set the color:</p>
  
  <p><code>"\x1b[30mblack\x1b[37mwhite"</code></p>
  
  <p>becomes</p>
  
  <p><code>@"&lt;span style=""color:#000000""&gt;black&lt;span style=""color:#BBBBBB""&gt;white&lt;/span&gt;&lt;/span&gt;"</code></p>
  
  <p>However I can not find a way to bind this text to my views while also reproducing the colors.</p>
  
  <p>Most examples online focus on situations where the text is always going to be the same, so colors can be hardcoded in the XAML and the text is bound to different spans/runs. This is not going to work for me.</p>
  
  <p>I have looked briefly at using the WebBrowser control but this seems like a very big hammer for such a small problem. I'm going to have hundreds of these labels in a list, so performance is a concern.</p>
  
  <p>Finally I found a solution whereby XAML may be written into the view at runtime, but I have had no luck whatsoever getting my converted string to load as valid XAML: <a href="https://stackoverflow.com/questions/343468/richtextbox-wpf-binding">Richtextbox wpf binding</a></p>
  
  <p>This seems to be one of those cases of WPF making a simple problem extremely difficult. Is there a good solution that I am overlooking?</p>