【问题标题】:* Sized columns not working properly* 大小列不能正常工作
【发布时间】:2013-12-14 10:02:22
【问题描述】:

我正在尝试打印一个页面。

这是我的代码:

private void Print(object obj)
{

    PrintDialog dialog = new PrintDialog();
    //if (dialog.ShowDialog() != true)
    //{ return; }

    Grid HaemogramReport = new Grid();

        RowDefinition rowTopMargin = new RowDefinition()
        {
            Height = new GridLength(198)
        };
        RowDefinition rowPatientDetails = new RowDefinition()
        {
            Height = new GridLength(1, GridUnitType.Star)
        };
        RowDefinition rowReportHeader = new RowDefinition()
        {
            Height = new GridLength(66)
        };
        RowDefinition rowBody = new RowDefinition()
        {
            Height = new GridLength(8, GridUnitType.Star)
        };
        RowDefinition rowBottomMargin = new RowDefinition()
        {
            Height = new GridLength(132)
        };

        HaemogramReport.RowDefinitions.Add(rowTopMargin);
        HaemogramReport.RowDefinitions.Add(rowPatientDetails);
        HaemogramReport.RowDefinitions.Add(rowReportHeader);
        HaemogramReport.RowDefinitions.Add(rowBody);
        HaemogramReport.RowDefinitions.Add(rowBottomMargin);

        Grid Head = new Grid();                

            Grid.SetRow(Head, 2);

            ColumnDefinition colLeftMargin = new ColumnDefinition()
            {
                Width = new GridLength(1, GridUnitType.Star)
            };
            ColumnDefinition colHead = new ColumnDefinition()
            {
                Width = GridLength.Auto
            };
            ColumnDefinition colRightMargin = new ColumnDefinition()
            {
                Width = new GridLength(1, GridUnitType.Star)
            };

            Head.ColumnDefinitions.Add(colLeftMargin);
            Head.ColumnDefinitions.Add(colHead);
            Head.ColumnDefinitions.Add(colRightMargin);

            TextBlock txtblockReportHeader = new TextBlock()
            {
                FontSize = 24,
                FontWeight = FontWeights.Bold,
                Text = "HAEMOGRAM REPORT",
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            var measures = MeasureString(txtblockReportHeader);

            Rectangle rectReportHeader = new Rectangle()
            {
                Stroke = Brushes.Black,
                StrokeThickness = 1,
                Width = measures.Width + 15,
                Height = measures.Height + 3,
                RadiusX = 7,
                RadiusY = 7,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            Grid.SetColumn(rectReportHeader, 1);
            Grid.SetColumn(txtblockReportHeader, 1);

            Head.Children.Add(rectReportHeader);
            Head.Children.Add(txtblockReportHeader);

        HaemogramReport.Children.Add(Head);

    HaemogramReport.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
    HaemogramReport.Arrange(new Rect(new Point(0, 0), HaemogramReport.DesiredSize));

    dialog.PrintVisual(HaemogramReport, "Haemogram Report");
}

private Size MeasureString(TextBlock textBlock)
{
    var formattedText = new FormattedText(
        textBlock.Text,
        CultureInfo.CurrentUICulture,
        FlowDirection.LeftToRight,
        new Typeface(textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch),
        textBlock.FontSize,
        Brushes.Black);

    return new Size(formattedText.Width, formattedText.Height);
}

这里是打印:

我希望矩形和文本块水平居中。但我认为 * 大小的列不能正常工作。

【问题讨论】:

    标签: c# wpf printing


    【解决方案1】:

    我知道了。

    我必须将网格的宽度设置为可打印区域的宽度。

    代码如下:

    Head.Width = dialog.PrintableAreaWidth;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-29
      • 2018-02-11
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多