WPF - FlowDocument - stretch a table to full width?

I have a DataTable containing an arbitrary number of columns and rows that I am trying to print. The luckiest thing I have had so far is to put the data in a table, and then add the table to FlowDocument.

So far so good. The problem I am facing right now is that the table only "wants" to occupy about half the width of the document. I already set the appropriate values โ€‹โ€‹for the FlowDocument PageWidth and ColumnWidth properties, but the table doesn't seem to want to stretch to fill the allocated space?

+4
source share
2 answers

I got lucky with this: How to set the original width of the WPF FlowDocument , although it took up about 90% of the space.

0
source

To set the contents of the FlowDocument to the full available widh, you must first find out the width of the page. A property that needs to be set that takes care of the length of the content is ColumnWidth support in the FlowDocument.

I usually create a helper class called โ€œPrintLayoutโ€ to save known presets for page width / height and padding. You can snif presets from MS Word and fill out more.

Class for PrintLayout

public class PrintLayout { public static readonly PrintLayout A4 = new PrintLayout("29.7cm", "42cm", "3.18cm", "2.54cm"); public static readonly PrintLayout A4Narrow = new PrintLayout("29.7cm", "42cm", "1.27cm", "1.27cm"); public static readonly PrintLayout A4Moderate = new PrintLayout("29.7cm", "42cm", "1.91cm", "2.54cm"); private Size _Size; private Thickness _Margin; public PrintLayout(string w, string h, string leftright, string topbottom) : this(w,h,leftright, topbottom, leftright, topbottom) { } public PrintLayout(string w, string h, string left, string top, string right, string bottom) { var converter = new LengthConverter(); var width = (double)converter.ConvertFromInvariantString(w); var height = (double)converter.ConvertFromInvariantString(h); var marginLeft = (double)converter.ConvertFromInvariantString(left); var marginTop = (double)converter.ConvertFromInvariantString(top); var marginRight = (double)converter.ConvertFromInvariantString(right); var marginBottom = (double)converter.ConvertFromInvariantString(bottom); this._Size = new Size(width, height); this._Margin = new Thickness(marginLeft, marginTop, marginRight, marginBottom); } public Thickness Margin { get { return _Margin; } set { _Margin = value; } } public Size Size { get { return _Size; } } public double ColumnWidth { get { var column = 0.0; column = this.Size.Width - Margin.Left - Margin.Right; return column; } } } 

Further on your FlowDocument you can set presets

On xaml

 <FlowDocument x:Class="WpfApp.MyPrintoutView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApp" mc:Ignorable="d" PageHeight="{Binding Height, Source={x:Static local:PrintLayout.A4}}" PageWidth="{Binding Width, Source={x:Static local:PrintLayout.A4}}" PagePadding="{Binding Margin, Source={x:Static local:PrintLayout.A4}}" ColumnWidth="{Binding ColumnWidth, Source={x:Static local:PrintLayout.A4}}" FontFamily="Segoe WP" FontSize="16" ColumnGap="4"> <!-- flow elements --> </FlowDocument> 

By code

 FlowDocument result = new WpfApp.MyPrintoutView(); result.PageWidth = PrintLayout.A4.Size.Width; result.PageHeight = PrintLayout.A4.Size.Height; result.PagePadding = PrintLayout.A4.Margin; result.ColumnWidth = PrintLayout.A4.ColumnWidth; 
+4
source

Source: https://habr.com/ru/post/1337880/


All Articles