Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF add an actual line to a FlowDocument

I am creating a FlowDocument programmatically. In this FlowDocument I want to add a line to break up the text. Not a new line of text but a line that goes from left to right. The issue being that FlowDocument width is dynamic so just adding in a string of "_" isn't really an option. Figured could add a line like this;

Line l = new Line();
l.Margin = new Thickness(3);
l.Height = 2;
l.Width = double.NaN;
FlowDocument doc = new FlowDocument();
doc.Blocks.Add(l);

Problem is that doc.Blocks.Add(l); returns this error;

cannot convert from 'System.Windows.Shapes.Line' to 'System.Windows.Documents.Block'

How can I fix this?


1 Answers

It should be possible with a BlockUIContainer.

A Line requires X1, Y1 and X2, Y2 settings. I don't think the auto-width with NaN will work.

You'll probably want to use a Rectangle with Height = 1 (and Stretch options) instead.

    var separator = new Rectangle();
    separator.Stroke = new SolidColorBrush(Colors.Blue);
    separator.StrokeThickness = 3;
    separator.Height = 3;
    separator.Width = double.NaN;

    var lineBlock = new BlockUIContainer(separator);       
    myFlowDocument.Blocks.Add(lineBlock);
like image 106
Henk Holterman Avatar answered Oct 25 '25 05:10

Henk Holterman