This way
- Create a
PrintDialog
: Instantiate a PrintDialog
object.
- Show the dialog: Call
ShowDialog()
on the PrintDialog
to display the print dialog box. This allows the user to select a printer and configure print settings.
- Print the visual: If the user selects "OK" in the print dialog, call
PrintVisual
on the PrintDialog
object, passing the window (or any other visual element) as the visual to be printed and a descriptive string for the job.
using System.Windows;
using System.Windows.Controls;
namespace PrintingExample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void PrintButton_Click(object sender, RoutedEventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
printDialog.PrintVisual(this, "My Window");
}
}
}
}
<Window x:Class="PrintingExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Print" Click="PrintButton_Click" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>