This is a migrated thread and some comments may be shown as answers.

Reporting + MVVM + WPF + DataSource

1 Answer 171 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Thorsten
Top achievements
Rank 1
Thorsten asked on 24 Sep 2019, 05:24 PM

Hi,

I have a ReportViewer control:

<tr:ReportViewer ReportSource="{Binding MyReportSource}"/>


A report:

public partial class MyReport : Telerik.Reporting.Report
{
    public MyReport()
    {
        InitializeComponent();
    }
}

 

A view model:

public class ViewModel
{
    public ReportSource MyReportSource { get; set; } = new InstanceReportSource { ReportDocument = new MyReport() };
    public IList<MyReportModel> MyModels { get; set; }
}

 

The ReportViewer control displays the structure defined in MyReport.

I assume that the ReportSource binding is working properly.

 

But how do i bind MyModels as a data source for MyReportSource,

so that it actually renders some data?

 

Thanks in advance,

machine spirit

 

1 Answer, 1 is accepted

Sort by
0
Thorsten
Top achievements
Rank 1
answered on 26 Sep 2019, 08:33 AM

I somehow figured (fiddled) it out...

 

The Report (MyReport.Designer.cs), had this defined:

private Telerik.Reporting.ObjectDataSource DataSource;

 

Seems to have been added by the designer.

Also it hides the 

Telerik.Reporting.Report.DataSource

property.

 

So i added one line to the Report constructor:

public partial class MyReport : Telerik.Reporting.Report
{
    public ReservationDetailsReport()
    {
        InitializeComponent();
        DataSource.DataSource = MyReportParameter.MyModels;
    }
}

 

And a new static class to act as a bucket for MyModels:

public static class MyReportParameter
{
    public static IList<MyModel> MyModels { get; set; }
}

 

In my ViewModel i had to do exatly the following:

public class ViewModel
{
    public IList<MyReportModel> MyModels { get; set; }
     
    private readonly MyReport myReport = new MyReport();
 
    public void UpdateReport()
    {
        MyReportSource = null;
         
        MyReportParameter.MyModels = MyModels;
 
        MyReportSource = new TypeReportSource
        {
            TypeName = myReport.GetType().AssemblyQualifiedName
        };
    }
}

 

For me none of this os obvious.

I have not found any hints to this in the documentation either.

Maybe this will help somebody.

 

Cheers!

Tags
General Discussions
Asked by
Thorsten
Top achievements
Rank 1
Answers by
Thorsten
Top achievements
Rank 1
Share this question
or