I just update my reporting here, and It won't return the DataContext...
that was my code:
private
ReportViewerModel Model
{
get
{
return
DataContext
as
ReportViewerModel; }
}
var layoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(
this
.ReportViewer1, 0);
var rvm = (ReportViewerModel)(layoutRoot.DataContext);
I need to call the DataContext to print my report.. I can see it, in reportviewer, but can't print...
There's something I can do?
3 Answers, 1 is accepted
If the report viewer template has not yet been created this could cause the unexpected behavior. To make sure the template is available use OnApplyTemplate or similar event:
public
partial
class
MainPage : UserControl
{
public
MainPage()
{
InitializeComponent();
}
public
override
void
OnApplyTemplate()
{
base
.OnApplyTemplate();
this
.ReportViewer1.PrintTheReport();
}
}
static
class
ReportViewerExtensions
{
public
static
void
PrintTheReport(thisReportViewer reportViewer)
{
var layoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(reportViewer, 0);
var viewerModel = (ReportViewerModel)layoutRoot.DataContext;
var printCommand = viewerModel.PrintReportCommand;
if
(printCommand.CanExecute(
null
))
{
printCommand.Execute(
null
);
}
}
}
If you are just after the report printing functionality you can avoid the Silverlight report viewer and implement your own Silverlight report printing functionality as shown in the following code snippet:
using
System;
using
System.Collections.Generic;
using
System.Collections.ObjectModel;
using
System.IO;
using
System.Text;
using
System.Windows.Controls;
using
System.Windows.Markup;
using
System.Windows.Printing;
using
Telerik.ReportViewer.Silverlight;
using
Telerik.Reporting.Service.SilverlightClient;
using
Telerik.Reporting.Service;
using
System.ComponentModel;
namespace
Reports
{
public
partial
class
MainPage1 : UserControl
{
string
reportName =
"Telerik.Reporting.Examples.CSharp.Invoice, CSharp.ReportLibrary"
;
List<Canvas> pages;
ReportServiceClient serviceClient;
string
instanceID;
PrintDocument printDocument;
int
pageCountToPrint;
public
MainPage1()
{
InitializeComponent();
this
.printDocument =
new
PrintDocument();
Uri serviceUri =
new
Uri(App.Current.Host.Source,
"../ReportService.svc"
);
this
.serviceClient =
new
ReportServiceClient(serviceUri);
this
.serviceClient.RenderAndCacheCompleted += RenderAndCacheCompleted;
this
.serviceClient.GetPageCompleted +=
new
EventHandler<GetPageEventArgs>(ServiceClientGetPageCompleted);
}
void
button1_Click(
object
sender, RoutedEventArgs e)
{
this
.button1.IsEnabled =
false
;
this
.pagesList =
new
Collection<Canvas>();
var parameters =
new
NameValueDictionary();
this
.serviceClient.RenderAndCacheAsync(
"XAML"
, reportName,
null
, parameters);
this
.pageCountToPrint = 0;
}
void
RenderAndCacheCompleted(
object
sender, RenderAndCacheEventArgs e)
{
this
.instanceID = e.RenderingResult.InstanceID;
int
pageCount;
for
(pageCount = 1; pageCount <= e.RenderingResult.PageCount; pageCount++, pageCountToPrint++)
{
this
.serviceClient.GetPageAsync(instanceID, pageCount);
}
this
.pages =
new
List<Canvas>(pageCount);
int
index = 0;
this
.printDocument.PrintPage += (s, arg) =>
{
var currentPrintPageNumber = ((PrintDocument)s).PrintedPageCount + 1;
arg.PageVisual =
this
.pages[currentPrintPageNumber-1];
arg.HasMorePages = (currentPrintPageNumber <
this
.pages.Count);
};
}
void
ServiceClientGetPageCompleted(
object
sender, GetPageEventArgs e)
{
var result = e.PageInfo;
using
(var ms =
new
MemoryStream(result.Buffer))
using
(var sr =
new
StreamReader(ms, Encoding.Unicode))
{
var pageRoot = XamlReader.Load(sr.ReadToEnd());
this
.pages.Add((Canvas)pageRoot);
}
pageCountToPrint--;
System.Diagnostics.Debug.WriteLine(e.PageInfo.PageNumber);
if
(
this
.pageCountToPrint == 0)
{
button2.IsEnabled =
true
;
}
}
void
button2_Click(
object
sender, RoutedEventArgs e)
{
this
.button1.IsEnabled =
true
;
this
.button2.IsEnabled =
false
;
this
.printDocument.Print(
"document"
);
}
}
}
If you experience any further difficulties we will appreciate if you open a support thread and send us the problematic project to review locally.
Greetings,Peter
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
public partial class ReportViewer : Page, IReportServiceClientFactory
{
public ReportSettingsDTO ReportParametersObject { get; set; }
public ReportViewer()
{
InitializeComponent();
this.rptViewer.ReportServiceClientFactory = this;
this.rptViewer.RenderBegin += new RenderBeginEventHandler(rptViewer_RenderBegin);
}
void rptViewer_RenderBegin(object sender, RenderBeginEventArgs args)
{
args.ParameterValues["ParameterObject"] = serialize();
}
public string serialize()
{
MemoryStream ms = new MemoryStream();
// Serializer the User object to the stream.
DataContractSerializer ser = new DataContractSerializer(typeof(ReportSettingsDTO));
ser.WriteObject(ms, ReportParametersObject);
byte[] array = ms.ToArray();
ms.Close();
return Encoding.UTF8.GetString(array, 0, array.Length);
}
#region IReportServiceClientFactory Members
ReportServiceClient IReportServiceClientFactory.Create(System.Uri remoteAddress)
{
var binding = new BasicHttpBinding()
{
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
ReceiveTimeout = new TimeSpan(0, 15, 0),
SendTimeout = new TimeSpan(0, 15, 0)
};
var endpointAddress = new EndpointAddress(remoteAddress);
return new ReportServiceClient(binding, endpointAddress);
}
#endregion
private void button2_Click(object sender, RoutedEventArgs e)
{
this.rptViewer.PrintTheReport();
}
}
static class ReportViewerExtensions
{
public static void PrintTheReport(this Telerik.ReportViewer.Silverlight.ReportViewer reportViewer)
{
var layoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(reportViewer, 0);
var viewerModel = (ReportViewerModel)layoutRoot.DataContext;
var printCommand = viewerModel.PrintReportCommand;
if (printCommand.CanExecute(null))
{
viewerModel.ApplyReportParametersCommand.Execute(null);
printCommand.Execute(null);
}
}
}
awaiting for the solution..
The NeedDataSource event is meant to be used only for providing data source to the data item and that data source is cached unless changed i.e. once cached the NeedDataSource event is not fired again and as you probably know every time the report is previewed, paged or exported through the viewer it is processed and rendered from scratch. During this processing, the cached data source is used, but the Values you have set in NeedDataSource are not being set as the event is not fired second time. That is why any other changes to report items or parameters should be moved to ItemDataBinding event, which is fired regardless.
All the best,Peter
the Telerik team
Q2’11 SP1 of Telerik Reporting is available for download (see what's new). Get it today.