ここ数日、私もこの問題を抱えていました。
解決策は、ルート要素をメモリにレンダリングすることでした。
PrintDialog dlg = new PrintDialog();
// Let it meassure to the printer's default width
// and use an infinity height
Grid1.Meassure(new Size(dlg.PrintableAreaWidth, double.PositiveInfinity));
// Let it arrange to the meassured size
Grid1.Arrange(new Rect(Grid1.DesiredSize));
// Update the element
Grid1.UpdateLayout();
次に、プリンターが使用する新しい用紙サイズを作成します。
プリンタのカット設定を確認する必要があります (Receiptカット モードを使用するなど)。
// Create a new papersize with the printer's default width, and the Grids height
dlg.PrintTicket.PageMediaSize
= new PageMediaSize(dlg.PrintableAreaWidth, Grid1.ActualHeight);
// Let's print !
dlg.PrintVisual(Grid1, "blah");
これは私にとって魅力のように機能し、多くのコードを節約できました。
レシートプリンター不要なのでpagination、とても使いやすいと思います。
XAML で作成されたものをレンダリングするためにこのメソッドを使用しないことに注意してください。すべてはルート要素としてUIElementStackPanelコードで作成されます。