在WPF中打印Datagrid记录 [英] Print Datagrid records in WPF
本文介绍了在WPF中打印Datagrid记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只需打印datagrid记录。我使用这样的一个代码,但是这个打印的datagrid滚动条也是。我只需要记录。
I need to print datagrid records only. I used one code like this, but this one printed datagrid scroll bar also. I need only records.
PrintDialog printDlg = new PrintDialog();
printDlg.PrintVisual(grid1, "Grid Printing.");
推荐答案
嗨对于WPF中打印DataGrid, code>< StackPanel> 并使用给定的代码。
Hey For Print DataGrid in WPF you have to take <StackPanel>
and use given code.
Xaml代码是
<StackPanel>
<DataGrid AutoGenerateColumns="False" Margin="12,0,0,0" Name="dataGrid1" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding}" AlternatingRowBackground="LightGoldenrodYellow" AlternationCount="1">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=Image}" Width="100" Height="50" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Make" Binding="{Binding Path=Make}"/>
<DataGridTextColumn Header="Model" Binding="{Binding Path=Model}"/>
<DataGridTextColumn Header="Price" Binding="{Binding Path=Price}"/>
<DataGridTextColumn Header="Color" Binding="{Binding Path=Color}"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="Print" Click="OnDataGridPrinting" Width="80" Height="30" />
</StackPanel>
而.CS代码是
private void OnDataGridPrinting(object sender, RoutedEventArgs e)
{
System.Windows.Controls.PrintDialog Printdlg = new System.Windows.Controls.PrintDialog();
if ((bool)Printdlg.ShowDialog().GetValueOrDefault())
{
Size pageSize = new Size(Printdlg.PrintableAreaWidth, Printdlg.PrintableAreaHeight);
// sizing of the element.
dataGrid1.Measure(pageSize);
dataGrid1.Arrange(new Rect(5, 5, pageSize.Width, pageSize.Height));
Printdlg.PrintVisual(dataGrid1, Title);
}
}
希望它有助于您
这是经过测试的代码。
这篇关于在WPF中打印Datagrid记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文