如何“扩展” RichTextBox段落的背景色?

时间:2018-11-18 16:06:54

标签: c# wpf

正如您在下面看到的,两侧都有白色边距,如何水平扩展灰色背景,因此它将TextBox从一端到另一端覆盖?

<RichTextBox x:Name="logTextBox" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto" IsReadOnly="True" FontSize="12"  Margin="10,165,10,10" >
            <RichTextBox.Resources>
                <Style TargetType="{x:Type Paragraph}">
                    <Setter Property="Margin" Value="0"/>
                </Style>
                <Style TargetType="ScrollViewer">
                    <Setter Property="MaxWidth" Value="480" />
                </Style>
            </RichTextBox.Resources>
        </RichTextBox>

用法:

    public void AddLog(string log)
    {
        Run run = new Run(log);
        Paragraph paragraph = new Paragraph(run);
        paragraph.Background = new SolidColorBrush(Colors.Gray);
        var numberOfBlocks = logTextBox.Document.Blocks.Count;
        const int MaxNumberOfBlocks = 100;
        if (numberOfBlocks > MaxNumberOfBlocks)
        {
            logTextBox.Document.Blocks.Remove(logTextBox.Document.Blocks.FirstBlock);
        }
        logTextBox.Document.Blocks.Add(paragraph);
        logTextBox.ScrollToEnd();
    }

enter image description here

1 个答案:

答案 0 :(得分:2)

在加载RichTextBox之后,将std::stod的{​​{1}}属性设置为PagePadding

FlowDocument