using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.Windows.Printing;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            DataContext = (from i in Enumerable.Range(0, 50)
                           select new MyObject() 
                           {
                               ID = i
                           }).ToList();
        }

        double offsetY;
        double totalHeight;
        Canvas canvas;
        RadGridView grid;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            offsetY = 0d;
            totalHeight = 0d;

            grid = new RadGridView();
            grid.DataContext = RadGridView1.DataContext;
            grid.ItemsSource = RadGridView1.ItemsSource;
            grid.RowIndicatorVisibility = Visibility.Collapsed;
            grid.ShowGroupPanel = false;
            grid.CanUserFreezeColumns = false;
            grid.IsFilteringAllowed = false;
            grid.AutoExpandGroups = true;
            grid.AutoGenerateColumns = false;

            foreach (GridViewColumn column in RadGridView1.Columns.OfType<GridViewColumn>())
            {
                GridViewColumn newColumn = (GridViewColumn)Activator.CreateInstance(column.GetType());
                newColumn.CopyPropertiesFrom(column);
                grid.Columns.Add(newColumn);
            }

            StyleManager.SetTheme(grid, StyleManager.GetTheme(RadGridView1));

            grid.SortDescriptors.AddRange(RadGridView1.SortDescriptors);
            grid.GroupDescriptors.AddRange(RadGridView1.GroupDescriptors);
            grid.FilterDescriptors.AddRange(RadGridView1.FilterDescriptors);

            ScrollViewer.SetHorizontalScrollBarVisibility(grid, ScrollBarVisibility.Hidden);
            ScrollViewer.SetVerticalScrollBarVisibility(grid, ScrollBarVisibility.Hidden);

            PrintDocument doc = new PrintDocument();

            canvas = new Canvas();
            canvas.Children.Add(grid);

            doc.PrintPage += this.doc_PrintPage;
            doc.Print("RadGridView print");
        }

        void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.PageVisual = canvas;

            if (totalHeight == 0)
            {
                totalHeight = grid.DesiredSize.Height;
            }

            Canvas.SetTop(grid, -offsetY);

            offsetY += e.PrintableArea.Height;
            e.HasMorePages = offsetY <= totalHeight;
        }

    }

    public class MyObject
    {
        public int ID { get; set; }
    }
}
