Search code examples
wpfmarkup

WPF Markup Control


I have a string of text that I need to display. I currently display it in a textbox.

My requirements have changed and now I need to display parts of this string in Red.

I have looked and all I can see for displaying text in several colors is a WebBrowser or RichTextbox. Both of these are more complex then I was hoping to use.

I can format the text string in any way (using any kind of markup).

Is there some kind of simple markup control out there for wpf? (Note: This will be going on a datagrid that can have many hundreds of rows, so it cannot be a memory/processing intensive control.)


Solution

  • You can use a TextBlock if it doesn't need to be edited.

    From above link:

    TextBlock is designed to be lightweight, and is geared specifically at integrating small portions of flow content into a user interface (UI). TextBlock is optimized for single-line display, and provides good performance for displaying up to a few lines of content.

    TextBlock is not optimized for scenarios that need to display more than a few lines of content; for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance. After TextBlock, FlowDocumentScrollViewer is the next lightest-weight control for displaying flow content, and simply provides a scrolling content area with minimal UI. FlowDocumentPageViewer is optimized around "page-at-a-time" viewing mode for flow content. Finally, FlowDocumentReader supports the richest set functionality for viewing flow content, but is correspondingly heavier-weight.

    Modified example from link showing Red Text:

    <Grid>
        <TextBlock TextWrapping="Wrap" >
            <Bold>TextBlock</Bold> is designed to be <Italic>lightweight</Italic>
                  and is geared  <Run Foreground="Red">specifically</Run> at integrating 
                  <Italic>small</Italic> portions of flow content into a UI.
    
        </TextBlock>
    </Grid>
    

    enter image description here