类别:程序开发

日期:2023-08-30 浏览:1656 评论:0

这里主要以类似图片格式方式打印,可以打印简单的DataGrid表格。方法如下:

将表格加入到画布中,然后调用打印机,将画布直接打印出来,具体代码如下:

单独用一个窗口放画布,将表格传进来,因为我的数据是绑定的DataTable上的数据,故给它单独加个表头并绑定(说白了就是换表头)。

xmal:

<Window x:Class="MyControlLibrarys.PrintDataWind"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="打印窗口" Height="600" Width="850">
    <Grid>
        <Canvas x:Name="printArea">
            <DataGrid Name="dgData" AutoGenerateColumns="False" CanUserAddRows="False" GridLinesVisibility="All" Margin="20">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="序号" Width="70" Binding="{Binding Path=id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                    <DataGridTextColumn Header="测试步骤" Width="140" Binding="{Binding Path=step, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                    <DataGridTextColumn Header="测试内容" Width="140" Binding="{Binding Path=content, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                    <DataGridTextColumn Header="标准" Width="120" Binding="{Binding Path=standard, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                    <DataGridTextColumn Header="实测" Width="120" Binding="{Binding Path=actual, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                    <DataGridTextColumn Header="结论" Width="120" Binding="{Binding Path=conclusion, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Canvas>
    </Grid>
</Window>

后台代码:

/// <summary>
    /// PrintDataWind.xaml 的交互逻辑
    /// </summary>
    public partial class PrintDataWind : Window
    {
        public PrintDataWind(System.Data.DataTable dt)
        {
            InitializeComponent();
            this.dgData.ItemsSource = dt.DefaultView;
        }
 
        public void print()
        {
            PrintDialog dialog = new PrintDialog();
            if (dialog.ShowDialog() == true)
            {
                dialog.PrintVisual(printArea, "Print Data");
            }
        }
    }

调用代码:

部分xmal(目的为了保持一致):

<DataGrid Grid.Row="1"  ColumnHeaderStyle ="{DynamicResource DataGridColumnHeaderStyle}" Grid.Column="0" Name="dgData" AutoGenerateColumns="False" CanUserAddRows="False" GridLinesVisibility="All">
            <DataGrid.Columns>
                <DataGridTextColumn Width="0.8*" Header="序号" ElementStyle="{StaticResource contentCenterStyle}" Binding="{Binding Path=OrderId, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <!--DataGridTextColumn Header="序号" Width="*" Binding="{Binding Path=id, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></-->
                <DataGridTextColumn Header="测试步骤" Width="2*" Binding="{Binding Path=step, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <DataGridTextColumn Header="测试内容" Width="2*" Binding="{Binding Path=content, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <DataGridTextColumn Header="标准" Width="2*" Binding="{Binding Path=standard, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <DataGridTextColumn Header="实测" Width="2*" Binding="{Binding Path=actual, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></DataGridTextColumn>
                <!--DataGridTextColumn Header="结论" Width="2*" Binding="{Binding Path=conclusion, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Foreground="{Binding Path=conclusion,ConverterParameter=通过, Converter={StaticResource DataColorConverterStyle}}"></-->
                <DataGridTemplateColumn Width="2*" Header="结论">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=conclusion,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" 
                                       Foreground="{Binding Path=conclusion,Converter={StaticResource DataColorConverterStyle}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

后台调用代码:

/// <summary>
        /// 打印数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintData_Click(object sender, RoutedEventArgs e)
        {
            PrintDataWind pdw = new PrintDataWind(_dt);
            pdw.print();
        }

这样就可以打印了,方法虽笨,但管用,该方法还可打印图片等。


本文标题:WPF打印Datagrid表格
本文链接:https://vtzw.com/post/1097.html
作者授权:除特别说明外,本文由 零一 原创编译并授权 零一的世界 刊载发布。
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。
 您阅读本篇文章共花了: 

历史上的今天
08月
30

 可能感兴趣的文章

评论区

发表评论 / 取消回复

必填

选填

选填

◎欢迎讨论,请在这里发表您的看法及观点。

最新留言