views:

53

answers:

1

Hey, sorry for my bad english... The default for a RichTextBox content is to inherit the Foreground color from the richtextbox itself. Thats nice, but if i set an especific color to some part of my text, that part do not inherit the foreground anymore, obviously. Now how can I make a part of that "colored" text inherit the parent color again? I'm trying to do something like the "Automatic" color from Office Word but after I have set a specific color to a TextRange, I do not know how to unset it :/

TextRange.ClearAllProperties() do what I need, but also erase other properties like fontSize and fontFamily...

TextRange.ApplyPropertyValue(ForegroundProperty, DependencyProperty.UnsetValue) also do not do the trick...

+1  A: 

This seemed almost impossible to achieve since there is no "RemovePropertyValue" method. I also tried with span and got the same exception as you did so I made a method that collects all the Paragraphs within the TextRange and made a span for each separetly.. less than ideal, I know.. Anyway, it works for a small example but might be pretty hard to work with for something more complex.

private List<Span> m_spanList = new List<Span>();

private void c_setForegroundButton_Click(object sender, RoutedEventArgs e)
{
    TextPointer textPointerStart = c_richTextBox1.Selection.Start;
    TextPointer textPointerEnd = c_richTextBox1.Selection.End;
    TextRange textRange = new TextRange(textPointerStart, textPointerEnd);
    SetForeground(textRange);
}

private void c_clearForegroundButton_Click(object sender, RoutedEventArgs e)
{
    foreach (Span span in m_spanList)
    {
        span.ClearValue(Span.ForegroundProperty);
    }
}

public void SetForeground(TextRange textRange)
{
    List<Paragraph> spannedParagraphs = new List<Paragraph>();
    if (textRange.Start.Paragraph != null)
    {
        TextRange curRange = null;
        Block cur = textRange.Start.Paragraph;
        do
        {
            spannedParagraphs.Add(cur as Paragraph);
            // Get next range
            curRange = new TextRange(cur.ContentStart, cur.ContentEnd);
        } while ((textRange.End.Paragraph == null || !curRange.Contains(textRange.End.Paragraph.ContentEnd)) && (cur = cur.NextBlock) != null);
    }

    if (spannedParagraphs.Count == 1)
    {
        Span span = new Span(c_richTextBox1.Selection.Start, c_richTextBox1.Selection.End);
        span.Foreground = Brushes.Red;
        m_spanList.Add(span);
    }
    else
    {
        for (int i = 0; i < spannedParagraphs.Count; i++)
        {
            if (i == spannedParagraphs.Count - 1)
            {
                Paragraph paragraph = spannedParagraphs[i];
                // For some reason I get an exception here when I try this..
                //m_span = new Span(paragraph.ElementStart, c_richTextBox1.Selection.End);
                c_richTextBox1.Selection.Select(paragraph.ElementStart, c_richTextBox1.Selection.End);
                Span span = new Span(c_richTextBox1.Selection.Start, c_richTextBox1.Selection.End);
                span.Foreground = Brushes.Red;
                m_spanList.Add(span);
            }
            else if (i == 0)
            {
                Paragraph paragraph = spannedParagraphs[i];
                Span span = new Span(c_richTextBox1.Selection.Start, paragraph.ElementEnd);
                span.Foreground = Brushes.Red;
                m_spanList.Add(span);
            }
            else
            {
                Paragraph paragraph = spannedParagraphs[i];
                Span span = new Span(paragraph.ElementStart, paragraph.ElementEnd);
                span.Foreground = Brushes.Red;
                m_spanList.Add(span);
            }
        }
    }
}
Meleak
Thanks for the reply, the result of your code is exactly what I need, but that wasn't exactly what I was looking for. In my case the user create the text (on a richTextbox), so he can select more than just a specific paragraph (or only part of it). I already tried to create a Span from the selected textRange and "ClearValue(ForegroundProperty)" all the inlines of that span but if the user select more than one pargraph I get the error: "'start' and 'end' TextPointers are not in the same Paragraph." :(
Leo
Meleak, thanks... your code did the trick. I will take a look on the most complex documents but for now I think it will work fine.
Leo