locked
Printing the contents of a WPF ListView RRS feed

  • Question

  • Hi

    I have a WPF ListView with a GridView and I need to print its contents.

    The ListView.ItemsSource is a List<myDataClass>. The GridView has one GridViewColumn for each public property in the myDataClass. The GridView also has one extra column in which I have a button. When the button is clicked, another ListView is dynamically filled and shown next to the button (when the button is clicked again the ListView is hidden).

    I use an XpsDocumentWriter to send the visual to the printer but this way I only get one page of output. I am in the dark: can anyone help?

    Thanks

    Monday, March 9, 2009 2:55 PM

Answers

  • Hi,

     

    The following is an example of printing document and utilizing XpsDocumentWriter class.

    XAML code: 

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:local="clr-namespace:WpfXPS"

            x:Class="WpfXPS.Window1">

        <Window.Resources>

            <local:People x:Key="peopleKey" />

        </Window.Resources>

        <StackPanel>

            <DocumentViewer Margin="17,27,24,67" Width="200"

                            Height="200"

                            Name="documentViewer1">

                <FixedDocument>

                    <PageContent>

                        <FixedPage>

                            <ListView

                  ItemsSource="{Binding Source={StaticResource peopleKey}}"  >

                                <ListView.View>

                                    <GridView>

                                        <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" />

                                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />

                                        <GridViewColumn Header="Button" Width="140">

                                            <GridViewColumn.CellTemplate>

                                                <DataTemplate>

                                                    <TextBlock Visibility="Collapsed" />

                                                </DataTemplate>

                                            </GridViewColumn.CellTemplate>

                                        </GridViewColumn>

                                    </GridView>

                                </ListView.View>

                            </ListView>

                        </FixedPage>

                    </PageContent>

                </FixedDocument>

            </DocumentViewer>

            <Button Height="23"

                    Name="ShowWindow"

                    Click="OnSaveXPSThenPreview">Print Preview</Button>

        </StackPanel>

    </Window>

     

    In the code behind:

    using System;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Media;

    using System.IO;

    using System.Windows.Xps.Packaging;

    using System.Windows.Xps;

    using System.IO.Packaging;

    using System.Collections.ObjectModel;

    namespace WpfXPS

    {

        public partial class Window1 : System.Windows.Window

        {

            public Window1()

            {

                InitializeComponent();

            }

            public void Export(Uri path, FrameworkElement surface)

            {

                if (path == null) return;

                // Save current canvas transorm

                Transform transform = surface.LayoutTransform;

                // Temporarily reset the layout transform before saving

                surface.LayoutTransform = null;

                // Get the size of the canvas

                Size size = new Size(surface.Width, surface.Height);

                // Measure and arrange elements

                surface.Measure(size);

                surface.Arrange(new Rect(size));

                // Open new package

                Package package = Package.Open(path.LocalPath, FileMode.Create);

                // Create new xps document based on the package opened

                XpsDocument doc = new XpsDocument(package);

                // Create an instance of XpsDocumentWriter for the document

                XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

                // Write the canvas (as Visual) to the document

                writer.Write(surface);

                // Close document

                doc.Close();

                // Close package

                package.Close();

                // Restore previously saved layout

                surface.LayoutTransform = transform;

            }

            private void OnSaveXPSThenPreview(object sender, RoutedEventArgs e)

            {

                //Save it as XPS file format

                Export(new Uri(@"C:\file.XPS", UriKind.Absolute), documentViewer1);

                // show it in a new window

                string filename = @"C:\file.XPS";

                DocumentViewer viewer = new DocumentViewer();

                XpsDocument doc = new XpsDocument(filename, FileAccess.Read);

                viewer.Document = doc.GetFixedDocumentSequence();

                //showWindow is a new window file in ShowWindow.xaml

                Window ShowWindow = new Window();

                ShowWindow.Width = 400;

                ShowWindow.Height = 300;

                ShowWindow.Content = viewer;

                ShowWindow.Show();

            }

        }

        public class People : ObservableCollection<Person>

        {

            public People()

            {

                for (int i = 0; i < 10; i++)

                {

                    this.Add(new Person() { ID = i.ToString(), Name = "Name" + i.ToString(), Description = "Description" + i.ToString() });

                }

            }

        }

        public class Person

        {

            public string ID { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }

        }

    }

     

    Sidenote:  The Export method is referred from this link:

    http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!240.entry

     

    Hope this helps.

     

    Thanks.


    Jim Zhou -MSFT
    Thursday, March 12, 2009 8:58 AM

All replies

  • I think it has to do with the GridView/ListView by default using VirtualizingStackPanel. Try disabling the VirtualizingStackPanel and printing. If it works, then you can think about finding a workaround or only disabling it when printing and enable it back again.
    Research and Development Imageright Inc. http://krishnabhargav.blogspot.com
    Monday, March 9, 2009 3:01 PM
  • Hi,

     

    The following is an example of printing document and utilizing XpsDocumentWriter class.

    XAML code: 

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:local="clr-namespace:WpfXPS"

            x:Class="WpfXPS.Window1">

        <Window.Resources>

            <local:People x:Key="peopleKey" />

        </Window.Resources>

        <StackPanel>

            <DocumentViewer Margin="17,27,24,67" Width="200"

                            Height="200"

                            Name="documentViewer1">

                <FixedDocument>

                    <PageContent>

                        <FixedPage>

                            <ListView

                  ItemsSource="{Binding Source={StaticResource peopleKey}}"  >

                                <ListView.View>

                                    <GridView>

                                        <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" />

                                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />

                                        <GridViewColumn Header="Button" Width="140">

                                            <GridViewColumn.CellTemplate>

                                                <DataTemplate>

                                                    <TextBlock Visibility="Collapsed" />

                                                </DataTemplate>

                                            </GridViewColumn.CellTemplate>

                                        </GridViewColumn>

                                    </GridView>

                                </ListView.View>

                            </ListView>

                        </FixedPage>

                    </PageContent>

                </FixedDocument>

            </DocumentViewer>

            <Button Height="23"

                    Name="ShowWindow"

                    Click="OnSaveXPSThenPreview">Print Preview</Button>

        </StackPanel>

    </Window>

     

    In the code behind:

    using System;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Media;

    using System.IO;

    using System.Windows.Xps.Packaging;

    using System.Windows.Xps;

    using System.IO.Packaging;

    using System.Collections.ObjectModel;

    namespace WpfXPS

    {

        public partial class Window1 : System.Windows.Window

        {

            public Window1()

            {

                InitializeComponent();

            }

            public void Export(Uri path, FrameworkElement surface)

            {

                if (path == null) return;

                // Save current canvas transorm

                Transform transform = surface.LayoutTransform;

                // Temporarily reset the layout transform before saving

                surface.LayoutTransform = null;

                // Get the size of the canvas

                Size size = new Size(surface.Width, surface.Height);

                // Measure and arrange elements

                surface.Measure(size);

                surface.Arrange(new Rect(size));

                // Open new package

                Package package = Package.Open(path.LocalPath, FileMode.Create);

                // Create new xps document based on the package opened

                XpsDocument doc = new XpsDocument(package);

                // Create an instance of XpsDocumentWriter for the document

                XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

                // Write the canvas (as Visual) to the document

                writer.Write(surface);

                // Close document

                doc.Close();

                // Close package

                package.Close();

                // Restore previously saved layout

                surface.LayoutTransform = transform;

            }

            private void OnSaveXPSThenPreview(object sender, RoutedEventArgs e)

            {

                //Save it as XPS file format

                Export(new Uri(@"C:\file.XPS", UriKind.Absolute), documentViewer1);

                // show it in a new window

                string filename = @"C:\file.XPS";

                DocumentViewer viewer = new DocumentViewer();

                XpsDocument doc = new XpsDocument(filename, FileAccess.Read);

                viewer.Document = doc.GetFixedDocumentSequence();

                //showWindow is a new window file in ShowWindow.xaml

                Window ShowWindow = new Window();

                ShowWindow.Width = 400;

                ShowWindow.Height = 300;

                ShowWindow.Content = viewer;

                ShowWindow.Show();

            }

        }

        public class People : ObservableCollection<Person>

        {

            public People()

            {

                for (int i = 0; i < 10; i++)

                {

                    this.Add(new Person() { ID = i.ToString(), Name = "Name" + i.ToString(), Description = "Description" + i.ToString() });

                }

            }

        }

        public class Person

        {

            public string ID { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }

        }

    }

     

    Sidenote:  The Export method is referred from this link:

    http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!240.entry

     

    Hope this helps.

     

    Thanks.


    Jim Zhou -MSFT
    Thursday, March 12, 2009 8:58 AM
  • Hi Jim,

    I'm getting error like "Property 'Pages' does not support values of type 'PageContent'" when i try this example.I couldn't see <PageContent> under <FixedDocument>

    How can i get rid of it?
    Thursday, March 12, 2009 10:20 AM
  • Hi,

     

    It seems that the PageContent can not be generated by the Visual Studio IDE’s automatic intelligence. you can add it manually. It works fine for me. Did you added all the necessary assembly references?

     
    Hope this helps.
    Thanks.


    Jim Zhou -MSFT
    Friday, March 13, 2009 3:01 AM
  • What are the necessary references to provide?
    Monday, October 26, 2009 11:33 AM
  • Hi Jim Zhou,

    When I try to run this code, there are no errors which turn up but the in the browser i get an error"An error occurred in the application you were using" and the error information is:

    System.Security.Policy.PolicyException: Execution permission cannot be acquired.
       at System.Runtime.Hosting.ApplicationActivator.CreateInstanceHelper(AppDomainSetup adSetup)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Windows.Interop.PresentationApplicationActivator.CreateInstance(ActivationContext actCtx)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at System.Windows.Interop.ApplicationLauncherXappDebug.Initialize()
       at System.Windows.Interop.DocObjHost.MS.Internal.AppModel.IBrowserHostServices.Run(String path, String fragment, MimeType mime, String debugSecurityZoneURL, String applicationId, Object streamContainer, Object ucomLoadIStream, String userAgentString, Boolean isDebugMode, Boolean isBrowserLowIntegrityProcess, INativeProgressPage nativeProgressPage, String progressAssemblyName, String progressClassName, String errorAssemblyName, String errorClassName)

    -----------------------

    PresentationHost.exe v3.0.6920.1427 built by: SP - c:\WINDOWS\system32\PresentationHost.exe
    ntdll.dll v5.1.2600.5755 (xpsp_sp3_gdr.090206-1234) - C:\WINDOWS\system32\ntdll.dll
    kernel32.dll v5.1.2600.5781 (xpsp_sp3_gdr.090321-1317) - C:\WINDOWS\system32\kernel32.dll
    ADVAPI32.dll v5.1.2600.5755 (xpsp_sp3_gdr.090206-1234) - C:\WINDOWS\system32\ADVAPI32.dll
    RPCRT4.dll v5.1.2600.5795 (xpsp_sp3_gdr.090415-1241) - C:\WINDOWS\system32\RPCRT4.dll
    Secur32.dll v5.1.2600.5834 (xpsp_sp3_gdr.090624-1305) - C:\WINDOWS\system32\Secur32.dll
    MSVCR80.dll v8.00.50727.4053 - C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.4053_x-ww_e6967989\MSVCR80.dll
    msvcrt.dll v7.0.2600.5512 (xpsp.080413-2111) - C:\WINDOWS\system32\msvcrt.dll
    USER32.dll v5.1.2600.5512 (xpsp.080413-2105) - C:\WINDOWS\system32\USER32.dll
    GDI32.dll v5.1.2600.5698 (xpsp_sp3_gdr.081022-1932) - C:\WINDOWS\system32\GDI32.dll
    ole32.dll v5.1.2600.5512 (xpsp.080413-2108) - C:\WINDOWS\system32\ole32.dll
    mscoree.dll v2.0.50727.3053 (netfxsp.050727-3000) - c:\WINDOWS\system32\mscoree.dll
    SHLWAPI.dll v6.00.2900.5512 (xpsp.080413-2105) - C:\WINDOWS\system32\SHLWAPI.dll
    urlmon.dll v8.00.6001.18828 (longhorn_ie8_gdr.090826-1700) - C:\WINDOWS\system32\urlmon.dll
    OLEAUT32.dll v5.1.2600.5512 - C:\WINDOWS\system32\OLEAUT32.dll
    iertutil.dll v8.00.6001.18828 (longhorn_ie8_gdr.090826-1700) - C:\WINDOWS\system32\iertutil.dll
    SHELL32.dll v6.00.2900.5622 (xpsp_sp3_gdr.080617-1319) - C:\WINDOWS\system32\SHELL32.dll
    WININET.dll v8.00.6001.18828 (longhorn_ie8_gdr.090826-1700) - C:\WINDOWS\system32\WININET.dll
    Normaliz.dll v6.0.5441.0 (winmain(wmbla).060628-1735) - C:\WINDOWS\system32\Normaliz.dll
    IMM32.DLL v5.1.2600.5512 (xpsp.080413-2105) - C:\WINDOWS\system32\IMM32.DLL
    LPK.DLL v5.1.2600.5512 (xpsp.080413-2105) - c:\WINDOWS\system32\LPK.DLL
    USP10.dll v1.0420.2600.5512 (xpsp.080413-2105) - c:\WINDOWS\system32\USP10.dll
    comctl32.dll v6.0 (xpsp.080413-2105) - C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83\comctl32.dll
    uxtheme.dll v6.00.2900.5512 (xpsp.080413-2105) - C:\WINDOWS\system32\uxtheme.dll
    MSCTF.dll v5.1.2600.5512 (xpsp.080413-2105) - C:\WINDOWS\system32\MSCTF.dll
    CSSInjOpen.dll v5, 1000, 0, 0 - C:\Program Files\ColumbiaSoft\Document Locator\Client\CSSInjOpen.dll
    comdlg32.dll v6.00.2900.5512 (xpsp.080413-2105) - C:\WINDOWS\system32\comdlg32.dll
    CLBCATQ.DLL v2001.12.4414.700 - c:\WINDOWS\system32\CLBCATQ.DLL
    COMRes.dll v2001.12.4414.700 - c:\WINDOWS\system32\COMRes.dll
    VERSION.dll v5.1.2600.5512 (xpsp.080413-2105) - C:\WINDOWS\system32\VERSION.dll
    xpsp2res.dll v5.1.2600.5512 (xpsp.080413-2113) - c:\WINDOWS\system32\xpsp2res.dll
    ws2_32.dll v5.1.2600.5512 (xpsp.080413-0852) - c:\WINDOWS\system32\ws2_32.dll
    WS2HELP.dll v5.1.2600.5512 (xpsp.080413-0852) - c:\WINDOWS\system32\WS2HELP.dll
    dfshim.dll v2.0.50727.3053 (netfxsp.050727-3000) - c:\WINDOWS\system32\dfshim.dll
    mscorwks.dll v2.0.50727.3603 (GDR.050727-3600) - c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
    PresentationHostDLL.dll v3.0.6920.1427 built by: SP - c:\WINDOWS\Microsoft.NET\Framework\v3.0\WPF\PresentationHostDLL.dll
    PSAPI.DLL v5.1.2600.5512 (xpsp.080413-2105) - c:\WINDOWS\system32\PSAPI.DLL
    msi.dll v3.1.4001.5512 - c:\WINDOWS\system32\msi.dll
    actxprxy.dll v6.00.2900.5512 (xpsp.080413-2113) - C:\WINDOWS\system32\actxprxy.dll
    SXS.DLL v5.1.2600.5512 (xpsp.080413-2111) - c:\WINDOWS\system32\SXS.DLL
    PresentationHostProxy.dll v3.0.6920.1427 built by: SP - c:\WINDOWS\system32\PresentationHostProxy.dll
    ieproxy.dll v8.00.6001.18828 (longhorn_ie8_gdr.090826-1700) - C:\Program Files\Internet Explorer\ieproxy.dll
    msctfime.ime v5.1.2600.5512 (xpsp.080413-2105) - C:\WINDOWS\system32\msctfime.ime
    netapi32.dll v5.1.2600.5694 (xpsp_sp3_gdr.081015-1312) - c:\WINDOWS\system32\netapi32.dll
    mscorlib.ni.dll v2.0.50727.3603 (GDR.050727-3600) - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\mscorlib\7124a40b9998f7b63c86bd1a2125ce26\mscorlib.ni.dll
    System.ni.dll v2.0.50727.3053 (netfxsp.050727-3000) - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System\3de5bd01124463d7862bd173af90bc83\System.ni.dll
    WindowsBase.ni.dll v3.0.6920.1427 built by: SP - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\WindowsBase\204d6e5b335134f23ca37638b9227ecf\WindowsBase.ni.dll
    PresentationCore.ni.dll v3.0.6920.1427 built by: SP - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\PresentationCore\94badbd64df59de7da249f71da38b1c2\PresentationCore.ni.dll
    PresentationFramework.ni.dll v3.0.6920.1500 built by: QFE - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\PresentationFramewo#\58c7ac6b6054038dc9346d7ec8e32b4c\PresentationFramework.ni.dll
    wpfgfx_v0300.dll v3.0.6920.1500 built by: QFE - c:\WINDOWS\Microsoft.NET\Framework\v3.0\WPF\wpfgfx_v0300.dll
    System.Xml.ni.dll v2.0.50727.3082 (QFE.050727-3000) - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Xml\5913d3f81e77194ec833991b1047a532\System.Xml.ni.dll
    System.Drawing.ni.dll v2.0.50727.3053 (netfxsp.050727-3000) - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Drawing\abb2ac7e08bee026f857d8fa36f9fe6f\System.Drawing.ni.dll
    System.Windows.Forms.ni.dll v2.0.50727.3053 (netfxsp.050727-3000) - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Windows.Forms\d2ea8d76f015817db1607075812b555f\System.Windows.Forms.ni.dll
    System.Security.ni.dll v2.0.50727.3053 (netfxsp.050727-3000) - C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\System.Security\a9e9b885a6601469c4058375cc74d856\System.Security.ni.dll
    rsaenh.dll v5.1.2600.5507 (xpsp.080318-1711) - c:\WINDOWS\system32\rsaenh.dll
    CRYPT32.dll v5.131.2600.5512 (xpsp.080413-2113) - c:\WINDOWS\system32\CRYPT32.dll
    MSASN1.dll v5.1.2600.5875 (xpsp_sp3_gdr.090904-1413) - c:\WINDOWS\system32\MSASN1.dll
    cryptnet.dll v5.131.2600.5512 (xpsp.080413-2113) - c:\WINDOWS\system32\cryptnet.dll
    SensApi.dll v5.1.2600.5512 (xpsp.080413-2108) - c:\WINDOWS\system32\SensApi.dll
    USERENV.dll v5.1.2600.5512 (xpsp.080413-2113) - C:\WINDOWS\system32\USERENV.dll
    WINHTTP.dll v5.1.2600.5727 (xpsp_sp3_gdr.081215-1359) - c:\WINDOWS\system32\WINHTTP.dll
    WLDAP32.dll v5.1.2600.5512 (xpsp.080413-2113) - C:\WINDOWS\system32\WLDAP32.dll
    mswsock.dll v5.1.2600.5625 (xpsp_sp3_gdr.080620-1249) - C:\WINDOWS\system32\mswsock.dll
    hnetcfg.dll v5.1.2600.5512 (xpsp.080413-0852) - c:\WINDOWS\system32\hnetcfg.dll
    wshtcpip.dll v5.1.2600.5512 (xpsp.080413-0852) - C:\WINDOWS\System32\wshtcpip.dll
    NTDSAPI.DLL v5.1.2600.5512 (xpsp.080413-2113) - c:\WINDOWS\system32\NTDSAPI.DLL
    DNSAPI.dll v5.1.2600.5625 (xpsp_sp3_gdr.080620-1249) - c:\WINDOWS\system32\DNSAPI.dll
    kerberos.dll v5.1.2600.5834 (xpsp_sp3_gdr.090624-1305) - C:\WINDOWS\system32\kerberos.dll
    cryptdll.dll v5.1.2600.5512 (xpsp.080413-2113) - c:\WINDOWS\system32\cryptdll.dll
    Cabinet.dll v5.1.2600.5512 (xpsp.080413-2105) - c:\WINDOWS\system32\Cabinet.dll
    diasymreader.dll v8.0.50727.3053 (netfxsp.050727-3000) - c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\diasymreader.dll.

    I tried in another system and it works fine.

    Can you help? awaiting a quick reply.

    Thanks in advance,
    ruben

           

    Monday, October 26, 2009 1:56 PM
  • Hi Rubenp.a,

    -->I tried in another system and it works fine.
    Since you can run the program on another system, it proves that the program itself is right, so it is probably caused by the computer operating system. have you installed all the necessary packages for running WPF application on the machine you are running on?

    Thanks.
    Sincerely


    Jim Zhou -MSFT
    Tuesday, October 27, 2009 9:06 AM