1

I using C# WPF and I have richtextbox and I want to color some text in red, some in green and some in black. How to do so?

  • please consider this question. it may help you http://stackoverflow.com/questions/17971300/wpf-richtextbox-syntax-highlighting-issue – Saifeddine M Jan 24 '16 at 18:35

2 Answers2

1

Use FlowDocumentReader in your RichTextBox. Thus, you can use document classes: List, Paragraph, Section, Table, LineBreak, Figure, Floater and Span, and change their properties:

<FlowDocumentReader x:Name="myDocumentReader" Height="269.4">
<FlowDocument>
<Section Foreground = "Yellow" Background = "Black">
<Paragraph FontSize = "20">
Here are some fun facts about the WPF Documents API!
</Paragraph>
</Section>
<List x:Name="listOfFunFacts"/>
<Paragraph x:Name="paraBodyText"/>
</FlowDocument>
</FlowDocumentReader>

You can also fill and change properties of, here for example, List right in the code:

this.listOfFunFacts.Foreground = Brushes.Brown;
this.listOfFunFacts.FontSize = 14;
this.listOfFunFacts.MarkerStyle = TextMarkerStyle.Circle;
this.listOfFunFacts.ListItems.Add(new ListItem( new Paragraph(new Run("Sample Text"))));
CssHammer
  • 66
  • 7
0

I needed a "simple" TextBox with different color on each line, using MVVM. All the answers I found deal with the RichTextBox's document as a whole which is overcomplicated for my needs so I post this answer:

XAML

<RichTextBox>
    <FlowDocument>
        <components:BindableParagraph InlineList="{Binding Messages}" />
    </FlowDocument>
</RichTextBox>

BindableParagraph

internal class BindableParagraph : Paragraph
{
    public ObservableCollection<Inline> InlineList
    {
        get { return (ObservableCollection<Inline>)GetValue(InlineListProperty); }
        set { SetValue(InlineListProperty, value); }
    }
    
    public static readonly DependencyProperty InlineListProperty =
        DependencyProperty.Register("InlineList", typeof(ObservableCollection<Inline>), typeof(BindableParagraph), new UIPropertyMetadata(null, OnPropertyChanged));

    private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        BindableParagraph textBlock = sender as BindableParagraph;
        ObservableCollection<Inline> list = e.NewValue as ObservableCollection<Inline>;
        list.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(textBlock.InlineCollectionChanged);
    }

    private void InlineCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            int idx = e.NewItems.Count - 1;
            Inline inline = e.NewItems[idx] as Inline;
            this.Inlines.Add(inline);
        }
    }
}

VM

private ObservableCollection<Inline> _messages;
public ObservableCollection<Inline> Messages
{
    get { return _messages; }
    set { SetProperty(ref _messages, value); }
}


private void AddMessage(string message, MessageLevel messageLevel = MessageLevel.None)
{
    Messages.Add(new Run()
    {
        Foreground = messageLevel == MessageLevel.Error ? Brushes.Red : Brushes.Black,
        Text = message + Environment.NewLine,
    });
}

RichTextBox with text of different color on each line

Viktor
  • 334
  • 1
  • 3
  • 14