admin管理员组

文章数量:1410682

I can set Paragraph's background property when creating a paragraph instance and add it to richTextBox.Document.Blocks

var p = new Paragraph
{
    LineHeight = 1.8,
    Background = _bgHighlight ;
}

var m = new Run("test string");
p.Inlines.Add(m);

richTextBox.Document.Blocks.Add(p);

After inserted a paragraph into Blocks, the background will not be able to be changed dynamically.

internal static readonly SolidColorBrush _bgHighlight = new SolidColorBrush(Colors.DodgerBlue) { Opacity = .4 };

private static readonly LinearGradientBrush _bgNormalManual = new LinearGradientBrush(Colors.Wheat, Colors.Coral, new Point(0, 0), new Point(0, 1)) { Opacity = .5 };

richTextBox.PreviewMouseLeftButtonDown += (s, e) => {

    Paragraph p = null;
    if (e.OriginalSource is Paragraph x)
    {
        p = x;
    }
    else if (e.OriginalSource is Run y && y.Parent is Paragraph z)
    {
        p = z;
    }
    if (p != null)
    {
        p.Background = _bgNormalManual;
    }
};

I have searched around, just fond only TextRange's Background can be changed.

var textRange = new TextRange(p.ContentStart, p.ContentEnd);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, _bgNormal);

but, it only can change the text words's background, not paragraph's background.

I can set Paragraph's background property when creating a paragraph instance and add it to richTextBox.Document.Blocks

var p = new Paragraph
{
    LineHeight = 1.8,
    Background = _bgHighlight ;
}

var m = new Run("test string");
p.Inlines.Add(m);

richTextBox.Document.Blocks.Add(p);

After inserted a paragraph into Blocks, the background will not be able to be changed dynamically.

internal static readonly SolidColorBrush _bgHighlight = new SolidColorBrush(Colors.DodgerBlue) { Opacity = .4 };

private static readonly LinearGradientBrush _bgNormalManual = new LinearGradientBrush(Colors.Wheat, Colors.Coral, new Point(0, 0), new Point(0, 1)) { Opacity = .5 };

richTextBox.PreviewMouseLeftButtonDown += (s, e) => {

    Paragraph p = null;
    if (e.OriginalSource is Paragraph x)
    {
        p = x;
    }
    else if (e.OriginalSource is Run y && y.Parent is Paragraph z)
    {
        p = z;
    }
    if (p != null)
    {
        p.Background = _bgNormalManual;
    }
};

I have searched around, just fond only TextRange's Background can be changed.

var textRange = new TextRange(p.ContentStart, p.ContentEnd);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, _bgNormal);

but, it only can change the text words's background, not paragraph's background.

Share Improve this question asked 2 days ago longlong 661 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I have found a solution, using DrawingBrush. for example

<DrawingBrush x:Key="paragraphSelectedBg">
    <DrawingBrush.Drawing>
        <GeometryDrawing Brush="LightBlue">
            <GeometryDrawing.Geometry>
                <RectangleGeometry Rect="0,0,1,1"/>
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>

internal static readonly DrawingBrush _paragraphSelectedBg= Application.Current.FindResource("paragraphSelectedBg") as DrawingBrush;
p.Background = _paragraphUnselectedBg;

本文标签: WPFhow to dynamic change Paragraph39s backgroundStack Overflow