Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How do I print a page/ data grids in Silverlight 4. RSS

6 replies

Last post Sep 22, 2010 08:48 PM by lax4u

(0)
  • lax4u

    lax4u

    Member

    10 Points

    114 Posts

    How do I print a page/ data grids in Silverlight 4.

    Aug 11, 2010 05:20 PM | LINK

    How do I print a page/ data grids in Silverlight 4?

    I know Silverlight does come with PrintDocument class that I need to use but here are my scenarios:

    1> I need to print the page as it is. The page has a scroll bar, but while printing it should print the whole page. ( including the area which is not visible because of scroll bars)

    I tried to create dynamic stack panel and try to add all existing controls to the dynamic stack panel, but I get error saying children are already part of other collection. Can it be done using Deep Clone/Serialization? If yes the how?

     

    2>     How to handle data grid printing which has paging

     

    3>is there easy way to print currentpage/total number of pages

  • FuryDiamond

    FuryDiamond

    All-Star

    24033 Points

    4110 Posts

    Re: How do I print a page/ data grids in Silverlight 4.

    Aug 11, 2010 05:22 PM | LINK

    Please "Mark as Answer" if this post answered your question. :)

    Silverlight 5 3D Tutorials: http://silverlight.bayprince.com
    Blog: http://blog.bayprince.com
    Twitter: http://twitter.com/bayprince
  • lax4u

    lax4u

    Member

    10 Points

    114 Posts

    Re: Re: How do I print a page/ data grids in Silverlight 4.

    Aug 12, 2010 03:39 PM | LINK

    well the problem is, they are all printing item collection (like grid). if you dont have IEnumerable, this will not work. I am looking for something that will print a control on the page as it is. But it should also consider scrollbar issue.

  • srcodemonkey

    srcodemonkey

    Member

    45 Points

    13 Posts

    Re: Re: Re: How do I print a page/ data grids in Silverlight 4.

    Aug 12, 2010 08:15 PM | LINK

    The problem with those MS examples is really that they're printing from a data source, not from the UI which it seems to me is the more common printing scenario. You want to print your page more or less as it appears on the screen, right? But the SL4 printing system puts up a huge roadblock to that. You have to give it a single element containing the elements you want to print as children. But if those elements are from your UI tree, they already have parents and can't also be children of something else.

    So far the only solution I've found is to have 2 trees, one for display and one for printing. How cumbersome is that! I found code that cloned elements (which can't be done natively in SL,) and code that generated xaml from elements, which you could use to create a new element. (It would be nice to have that feature in SL too.) But I tried both, and the resulting elements just didn't render properly, or at all.

    As for text inside a scrollbar etc. - it's true, you're going to get the element exactly as it's rendered on screen. If you want to print the complete contents of a scrollviewer or scrolling textbox, you have to extract it from the container and put it in something else.

  • lax4u

    lax4u

    Member

    10 Points

    114 Posts

    Re: Re: Re: Re: How do I print a page/ data grids in Silverlight 4.

    Sep 15, 2010 04:56 PM | LINK

    here is what i did if anyone intrested. I created all my UI controls as Resources :):). Now i can use those resources anywhere i want. When user clicks on Print, I get the resource from Resource collection and bind it to the same ViewModel.

  • pallone

    pallone

    Member

    181 Points

    495 Posts

    Re: Re: Re: Re: How do I print a page/ data grids in Silverlight 4.

    Sep 16, 2010 08:37 AM | LINK

    Hi lax4u,

     

    Could you please share some sample code of how to add the UI controls as Resources and also how to get the resource you need from the collection and bind it to the ViewMode?

    Cheers

  • lax4u

    lax4u

    Member

    10 Points

    114 Posts

    Re: Re: Re: Re: How do I print a page/ data grids in Silverlight 4.

    Sep 22, 2010 08:48 PM | LINK

    I'm loading the DataTemplate into StackPanel programatically.

    public partial class Page1 : Page
        {
            ViewModel _vm = null;
            public Page1()
            {
                InitializeComponent();
                _vm = new ViewModel();
                _vm.FirstName = "James";
                LoadTemplate(spMain);
                spMain.DataContext = _vm;
            }       
    
            private void LoadTemplate(StackPanel sp)
            {
                DataTemplate res = (DataTemplate)Resources["Resource1"];
                FrameworkElement element = res.LoadContent() as FrameworkElement;
                sp.Children.Add(element);
            }
    
            StackPanel spPrint = null;
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                spPrint = new StackPanel();
                LoadTemplate(spPrint);
                spPrint.DataContext = _vm;
                PrintDocument doc = new PrintDocument();
                doc.PrintPage += new EventHandler<PrintPageEventArgs>(doc_PrintPage);
                doc.Print("Doc");
            }
    
            void doc_PrintPage(object sender, PrintPageEventArgs e)
            {
                Viewbox box = new Viewbox();
                box.StretchDirection = StretchDirection.Both;
                box.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                box.Child = spPrint;
                e.PageVisual = box;
            }
        }
     
     <UserControl.Resources>
            <DataTemplate x:Key="Resource1">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="FirstName"/>
                    <TextBox Text="{Binding Path=FirstName,Mode=TwoWay}" Width="100" Height="30"></TextBox>
                </StackPanel>
            </DataTemplate>
        </UserControl.Resources>
    
        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel x:Name="spMain">            
            </StackPanel>
            <Button Content="Print" Click="Button_Click" Width="100" Height="30"/>
        </Grid>


     

    But i'm sure there should be a way to set DataTemplate using Binding and StaticResources. eg you can use ContentControl and set TemplatePanel property.