开发者

Appying Background Color to entire line of Text

I have a RichTextBox in a WPF Application. I would like to apply alternate coloring to each lines of texts. I have set the Background property to be of the desired color, but due to differences in text length, only the portion containing text gets the background color. How to set properties so that the entire line is set to the desired background color.

One non-elegant solution is to, pad some 开发者_如何学Pythonspaces, but if RichTextBox layout changes, then the code needs to be changed by trial and error basis.

Any better approach?


If all the lines are the same height, then you can apply a background graphic to the RichTextBox itself, which would have the same effect (set dimensions / colours as appropriate):

<RichTexBox>
    <RichTexBox.Background>
            <VisualBrush TileMode="Tile" Viewport="0 0 100 100" ViewportUnits="Absolute">
                <VisualBrush.Visual>
                    <StackPanel>
                       <Rectangle Width="100" Height="50" Fill="Red" />
                       <Rectangle Width="100" Height="50" Fill="Blue" />
                    </StackPanel>
                </VisualBrush.Visual>
            </VisualBrush>
    </RichTexBox.Background>
</RichTexBox>


Try to use List which highlights the rows of lines with a background color. Refresh the ListItem collection (according to the number of lines in the text) when anyone changes Text in RichTextBox.TextChanged event.

    <RichTextBox>
        <RichTextBox.Document>
            <FlowDocument>
                <List>
                    <ListItem Background="Red" />
                    <ListItem Background="Green"/>
                    <ListItem Background="Yellow"/>
                </List>
            </FlowDocument>
        </RichTextBox.Document>
    </RichTextBox>

I think you can change the bulleted look n feel of the list by overriding its style.


I wanted to know how to make this work with Items generated dynamically. For my actual case, I used extra spaces. For now, this is the way for me to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜