MVVM_pattern_diagramThis is an area of a lot of question and hassle. During the Telerik Reporting Webinar for Quarter 2, 2012. One of the areas of inquiries was the use of Telerik Reporting with MVVM pattern.

It is possible you would find yourself in a scenario of implementing Telerik Reporting into an existing MVVM project. It is also possible that the requirements of this project consist of avoiding changes to the existing WPF views. The views may play a very important role in generating the report data. Below I provided a brief strategy for bringing Telerik Reporting and MVVM pattern together, when feeding a report from an existing view is necessary.

Best practice is to create the Report in a class library, this practice apply even when producing reports for MVVM pattern. The only key variation is to create a constructor capable of receiving the report parameters or objects needed for the report to render, i.e:

   1: public Report1(string _reportparameter)
   2: {
   3:     InitializeComponent();
   4:     this.Report.ReportParameters["ReportParmeter1"].Value = _reportparameter;
   5:     ...

1. Create a view for that contains the Telerik Reporting Report Viewer i.e. MyReportWindow.xaml, this window may be open by an existing WPF view.

2. In the existing view i.e. DataEntryForReportView.xaml you may add the following object.

   1: <TextBox Name="WindowReport" Visibility="Hidden" Tag="{Binding myReportWindow, Mode=TwoWay}"/>

3. In the constructor, and since views can only be seen by other views, assign the Report Window to the tag of the new created object

   1: public DataEntryForReportView ()
   2: {
   3:     InitializeComponent();
   4:     this.WindowReport.Tag = new MyReportWindow();
   6: }

4. In the view model of the above view i.e.  DataEntryForReportViewModel.cs add the notify property change event for this new property

   1: public RadWindow myReportWindow
   2: {
   3:     get { return _myreportwindow; }
   4:     set
   5:     {
   6:         _myreportwindow = value;
   7:         NotifyPropertyChanged("myReportWindow");
   8:     }
   9: }

5. In a click event of the data entry view, where we are to open the report window. We need to pass the report we created in the class library

   1: _myreportwindow.Tag = new MyReportClass.Report1(reportparameter);
   2: _myreportwindow.Show();

6. Prepare the Report Window - MyReportWindow.xaml so that the report viewer can render the Report Object passed on its tag property.

   1: private void RadWindow_Loaded(object sender, RoutedEventArgs e)
   2: {
   3:     if (this.Tag != null)
   4:     {
   5:         this.ReportViewer1.Report = (Telerik.Reporting.Report)this.Tag;
   6:         this.ReportViewer1.RefreshReport();
   7:         this.WindowState = WindowState.Maximized;
   9:     }