'Print WPF Visuals with MVVM pattern

My ViewModel has a PrintCommand executing a Method called PrintCalendar(). But the Calendar aka datagrid is in the View, so how do I get my datagrid into the ViewModel?

Getting my hands dirty and do all that stuff in code-behind? oh no...

PrintDialog printDlg = new PrintDialog();
printDlg.PrintVisual(datagrid, "Grid Printing.");


Solution 1:[1]

I guess we need the following to be able to show the print dialog box as well:

public RelayCommand<Visual> PrintCommand
{
    get
    {
        return new RelayCommand<Visual>( v =>
        {
            PrintDialog printDlg = new PrintDialog();

            if (Convert.ToBoolean(myPrintDialog.ShowDialog()))
            {
                printDlg.PrintVisual( v, "Grid Printing." );
            }
        });
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3884423