【问题标题】:WPF - FlowDocument - Stretch Table to Entire Width?WPF - FlowDocument - 将表格拉伸到整个宽度?
【发布时间】:2011-06-18 23:15:53
【问题描述】:

我有一个 DataTable,其中包含我要打印的任意数量的列和行。到目前为止,我最好的运气是将数据放入表中,然后将表添加到 FlowDocument。

到目前为止一切顺利。我现在遇到的问题是表格只“想要”占据文档宽度的大约一半。我已经为 FlowDocument 的 PageWidth 和 ColumnWidth 属性设置了适当的值,但是 Table 似乎不想拉伸以填满分配的空间?

【问题讨论】:

  • 我最终使用 FixedDocument 来使其工作,但如果有人对使用 FlowDocument 有一些见解,我仍然很想听听。

标签: wpf width flowdocument sizing


【解决方案1】:

我很幸运:How to set the original width of a WPF FlowDocument,虽然它只占用了大约 90% 的空间。

【讨论】:

  • 其实这10%的未使用空间是因为我在FlowDocument中添加了PagePadding="0.5in",这是为了防止打印时页面被切掉所必需的。
【解决方案2】:

为了将您的 FlowDocument 内容设置为完整的可用宽度,您必须首先知道页面的宽度。您需要设置的用于处理内容长度的属性是 FlowDocument 上的 ColumnWidth 属性。

我通常会创建一个“PrintLayout”帮助类来保留已知的页面宽度/高度和填充预设。您可以从 Ms Word 中嗅探预设并填充更多内容。

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;
        }
    }
}

接下来您可以在 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;

【讨论】:

  • 感谢您,这对我帮助很大。稍作修正:A4 纸张尺寸为 21cm x 29.7cm,A3 为 29.7cm x 42cm。
猜你喜欢
  • 2012-06-02
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 2012-02-04
相关资源
最近更新 更多