Autor Beitrag
funcry
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 110
Erhaltene Danke: 1

Win7 64, XP 32
C# (VS 2010 EE), Delphi (TD 2006 Win32)
BeitragVerfasst: Do 25.08.11 14:29 
Derzeit suche ich nach einer simplen Möglichkeit ein WPF-Datagrid zu drucken.

Der aktuelle Ansatz mittels dem folgendem Quellcode ist leider nicht praktikabel, da bei längeren Inhalten das Ergebnis fehlerhaft ist (Zeilen wiederholen sich).

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
private void MenuItemPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog printDlg = new PrintDialog();
            if (printDlg.ShowDialog() == true)
            {
                Size pageSize = new Size(printDlg.PrintableAreaWidth - 35, printDlg.PrintableAreaHeight - 35);
                this.dataGridQueryResult.Measure(pageSize);
                this.dataGridQueryResult.Arrange(new Rect(3535, pageSize.Width - 35, pageSize.Height - 35));
                printDlg.PrintVisual(this.dataGridQueryResult, this._fileName + " " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
            }
        }
funcry Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 110
Erhaltene Danke: 1

Win7 64, XP 32
C# (VS 2010 EE), Delphi (TD 2006 Win32)
BeitragVerfasst: Do 25.08.11 18:12 
Habs so gelöst....


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
private void MenuItemPrint_Click(object sender, RoutedEventArgs e)
        {
            this.dataGridQueryResult.SelectAllCells();
            this.dataGridQueryResult.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
            ApplicationCommands.Copy.Execute(nullthis.dataGridQueryResult);
            this.dataGridQueryResult.UnselectAllCells();            
            String result = (string)Clipboard.GetData(DataFormats.Html);
            Clipboard.Clear();

            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "context.html");
            File.WriteAllText(path, result);
            Process.Start(new ProcessStartInfo(path));
        }
UserNext
Hält's aus hier
Beiträge: 1



BeitragVerfasst: So 31.03.13 22:53 
Kann jemand bestätigen ob das so richtig ist?
Da bekomme ich doch nur eine html seite angezeigt aber wie drucke ich sie?

Moderiert von user profile iconTh69: Unnötige Full-Quotes entfernt.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4799
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 01.04.13 10:39 
Anscheinend dann von Hand im geöffneten Browser.
Man könnte aber auch dem Process Parameter mitgeben, z.B. "/p" oder "-print" o.ä. (leider unterstützen dies die gängigen Standard-Browser wie z.B. IE oder Firefox nicht - nur über spezielle Add-ons):
ausblenden C#-Quelltext
1:
Process.Start(new ProcessStartInfo(path, "/p"));					


Die bessere Alternative wäre m.E. also weiterhin der erste Weg über PrintVisual, nur daß man dann den beschriebenen Fehler bei mehrseitigem Druck noch beseitigen müßte.
Auf WPF Printing Overview wird folgendes vorgeschlagen:
ausblenden C#-Quelltext
1:
2:
3:
4:
printGrid.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight)); 
printGrid.Arrange(new Rect(new Point(5050), printGrid.DesiredSize)); 
 
dialog.PrintVisual(printGrid, "A WPF printing");