WPF - FlowDocument - 如何将表格拉伸至整个宽度?

8
我有一个包含任意行和列的DataTable,我想要将其打印出来。目前为止,我已经将数据放入Table中,然后将Table添加到FlowDocument中,效果还不错。但是,我现在遇到的问题是Table只想占据文档宽度的一半左右。虽然我已经设置了FlowDocument的PageWidth和ColumnWidth属性的适当值,但是Table似乎不想拉伸以填满所分配的空间?

我最终使用了FixedDocument使其工作,但如果有人对使用FlowDocument有一些见解,我仍然很愿意听取。 - Sonny Boy
2个回答

6
为了将您的FlowDocument内容设置为完整可用宽度,您首先需要知道页面的宽度。负责内容长度的属性是FlowDocument中的ColumnWidth属性。
我通常创建一个“PrintLayout”辅助类来保存已知的页面宽度/高度和填充的预设值。您可以从Ms Word中获取预设值并添加更多。
打印布局类如下:
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;
        }
    }
}

在您的FlowDocument中,接下来可以设置预设值。
在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>

按代码

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;

1
谢谢,这对我帮助很大。只有一个小修正:A4纸张尺寸是21cm x 29.7cm,A3是29.7cm x 42cm。 - solarc

0

实际上,10%未使用的空间是因为我在FlowDocument中添加了PagePadding="0.5in",这是为了在打印时保持页面不被切断所必需的。 - Kelly Cline

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接