Is it possible to seletively color a TextBlock wrapper in Silverlight / WPF

For example, if I have a TextBlock:

"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Assimilation and minimal stay, quis nostrud, an exercise performed according to need. Except that he was killed but not was killed.

I want the first 10% of this TextBlock, so the font color should be red and the rest should be green.

Perhaps this means that he will stain "Lorem ipsum dolor sit amet, consectetur adipisici" and part "n". Basically pixel coloring fonts instead of characters.

Another important behavior is that the percentage refers to the wrapper, because if 50% of the contents of a TextBlock is colored, it means that 50% of the text in reading order is colored, not the first half of the block.

For example, this is a similar question (with correct answers), but with regard to Label / TextBlocks without packaging: Is it possible to colorfully label a shortcut in Silverlight?

+5
source share
3 answers

I do not think that this is possible pixel by pixel (at least not without measuring the text and using one of the methods from the related question in turn). This can be done symbolically using the Run object:

<TextBlock FontSize="24" TextWrapping="Wrap">
  <Run Foreground="Red">Lorem ipsum dolor sit amet, consectetur</Run>
  adipisicing elit, sed do eiusmod tempor incididunt ut labore et
  dolore magna aliqua. Ut enim ad minim veniam...
</TextBlock>

, , Runs ( TextBlock.Inlines), .

+3

Runs, . , ...

    <TextBlock>
        <Run Background="Red">
            Republican
        </Run>
        <Run Background="Blue">
            Democrat
        </Run>
    </TextBlock>
+4

, . WPF , TextFormatter. Silverlight , , - , , , , . Eugh.

+2
source

All Articles