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

WPF: Binding ReportViewer.Report to Viewmodel property

10 Answers 1066 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Stefan Kamphuis
Top achievements
Rank 2
Stefan Kamphuis asked on 07 May 2010, 12:45 PM
Hi,

First of all, thanks for adding WPF support to Telerik.Reporting. Love it so far and am actually using it already in a clients project.

This project is a WPF MVVM project. So, using the samples I added this to my View.xaml:
<telerik:ReportViewer Name="reportViewer1" Margin="0" Report="{Binding Path=Report"/> 

Now, the ViewModel object contains the property report:
        private Report _report; 
 
        public Telerik.Reporting.Report Report 
        { 
            get 
            { 
                return _report; 
            } 
            set 
            { 
                // Check if it's really a change 
                if (value == _report) 
                    return
 
                // Change Report 
                _report = value; 
 
                // Notify attached View(s) 
                RaisePropertyChanged("Report"); 
            } 
        } 

Since the report get's its data from a business method (using a Webservice), I've put the code to create the report in the constructor for the ViewModel:
        #region Constructor 
        public SampleReportViewModel() 
        { 
            // get data and create report here 
            var report = new SampleReport(); 
            var dataObject = myClass.GetData(); 
            report.DataSource = dataObject; 
            Report = report; 
        } 
        #endregion 
 
Now, while this code works just fine when I put it in the codebehind for the View (xaml) file itself, it doesn't work when I'm using databinding my ViewModel to my View.

Any ideas on how to fix this?
Thanks!

Stefan Kamphuis


10 Answers, 1 is accepted

Sort by
0
Svetoslav
Telerik team
answered on 10 May 2010, 03:14 PM
Hi Stefan Kamphuis,

Attached you may find an example that illustrates how to bind the Report Viewer in a MVVM scenario. The most interesting part is that the ReportViewer control has already its own data context as it is build on the MVVM pattern itself (so is the Silverlight report viewer control). So you could not expect to use other data context.

In the provided example, the Window object is connected to the view model and the ReportViewer is bound to the view model through the windows's data context:

<Window x:Class="CSharp.WpfDemo.Window2"
    xmlns:telerik="clr-namespace:Telerik.ReportViewer.Wpf;assembly=Telerik.ReportViewer.Wpf"
    xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
    xmlns:telerikControls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
    x:Name="thisWindow"
    Title="Window2" Height="300" Width="300">
    <Grid>   
        <telerik:ReportViewer x:Name="ReportViewer1" Report="{Binding DataContext.Report, ElementName=thisWindow}" />
    </Grid>
</Window>

All the best,
Svetoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Gregory
Top achievements
Rank 1
answered on 21 Feb 2011, 11:37 PM
Why doesn't the ReportViewer automatically inherit the DataContext through the logical tree (ie, the usercontrol's DataContext is set to my ViewModel, but I still have to manually set the ElementName=ViewModel).
0
Chavdar
Telerik team
answered on 24 Feb 2011, 09:35 AM
Hi Gregory,

The WPF report viewer is built using the MVVM pattern and has its own DataContext which it is bound to. This is the reason why the parent DataContext is not inherited.

Kind regards,
Chavdar
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
William
Top achievements
Rank 1
answered on 27 Jun 2012, 10:27 PM
I came across this thread while trying to bind the ReportViewer to my ViewModel and tried several variations of the binding examples above including the one listed below.  But I kept getting that same error: "A 'Binding' cannot be set on the 'ReportDocument' property of type 'InstanceReportSource'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject." 

I'm currently using Telerik.Reporting version 6.1.12.611

Why are we getting this?

<UserControl x:Class="MyCompanyName.MyProductName.ReportOverlayView"
        xmlns:tr="http://schemas.telerik.com/wpf"
        xmlns:telerikReporting="clr-namespace:Telerik.Reporting;assembly=Telerik.Reporting">
    <Grid>
        <tr:ReportViewer Grid.Row="1"
                             x:Name="ReportViewer1"
                             telerik:StyleManager.Theme="{Binding SelectedItem, ElementName=ThemeSelector}" HorizontalAlignment="Stretch">
            <tr:ReportViewer.ReportSource>
                <telerikReporting:InstanceReportSource ReportDocument="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.Report }" />
            </tr:ReportViewer.ReportSource>
        </tr:ReportViewer>
    </Grid>
</UserControl>
0
IvanY
Telerik team
answered on 03 Jul 2012, 11:32 AM
Hello William,

You can only bind to the ReportSource property; in your ViewModel you can pass the respective report source (InstanceReportSource in your case). It has to be something like this:
 
<telerik:ReportViewer x:Name="ReportViewer1" ReportSource="{Binding MyReportSource}" />

In your ViewModel then you pass to that property your InstanceReportSource.

Greetings,
IvanY
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

0
Muhammad
Top achievements
Rank 1
answered on 29 Jul 2014, 07:15 PM
HI Admin,
I am trying to create reports in WPF using telerik report view. I am using busines object as data source but I am not able to show any data on report. I am sending you my simple project can you please take a look at it and see what I am missing .
Here is my code

/// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Creating and configuring the ObjectDataSource component:
            Telerik.Reporting.ObjectDataSource objectDataSource = new Telerik.Reporting.ObjectDataSource();
            objectDataSource.DataSource = typeof(Products); // Specifying the business object type
            objectDataSource.DataMember = "GetProducts"; // Specifying the name of the data object method

            objectDataSource.CalculatedFields.Add(new Telerik.Reporting.CalculatedField("FullName", typeof(string), "=Fields.Name + ' ' + Fields.ProductNumber")); // Adding a sample calculated field.

            // Specify the parameters, their types and values
            objectDataSource.Parameters.Add(new Telerik.Reporting.ObjectDataSourceParameter("color", typeof(string), "Silver"));
            objectDataSource.Parameters.Add(new Telerik.Reporting.ObjectDataSourceParameter("productModelID", typeof(int), 23));

            // Creating a new report
            Telerik.Reporting.Report report = new Telerik.Reporting.Report();

            // Assigning the ObjectDataSource component to the DataSource property of the report.
            report.DataSource = objectDataSource;

            // Use the InstanceReportSource to pass the report to the viewer for displaying
            Telerik.Reporting.InstanceReportSource reportSource = new Telerik.Reporting.InstanceReportSource();
            reportSource.ReportDocument = report;

            // Assigning the report to the report viewer.
            reportViewer1.ReportSource = reportSource;

            // Calling the RefreshReport method in case this is a WinForms application.
            reportViewer1.RefreshReport();
            //this.reportViewer1.ReportSource = typeReportSource;
        }
    }

    public class Product
    {
        public string Name { get; set; }
        public string ProductNumber { get; set; }
        public decimal ListPrice { get; set; }
        public int ProductModelID { get; set; }
        public string Color { get; set; }
    } 
public class Products
    {

        [DataObjectMethod(DataObjectMethodType.Select)]
        public Product[] GetArraySource()
        {
            return this.GetAllProducts().ToArray();
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public ArrayList GetArrayListSource()
        {
            ArrayList arrayList = new ArrayList();
            foreach (var product in this.GetAllProducts())
            {
                arrayList.Add(product);
            }
            return arrayList;
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public List<Product> GetAllProducts()
        {
            
         
            List<Product> products = new List<Product>();

           
                
                    products.Add(new Product()
                    {
                        Name = "Borehole Diameter",
                        ProductNumber = "11111111111",
                        ListPrice = 298,
                        ProductModelID = 7,
                        Color = "Red"
                    });
                    products.Add(new Product()
                    {
                        Name = "Borehole Diameter",
                        ProductNumber = "11111111111",
                        ListPrice = 298,
                        ProductModelID = 7,
                        Color = "Red"
                    });
                    products.Add(new Product()
                    {
                        Name = "Borehole Diameter",
                        ProductNumber = "11111111111",
                        ListPrice = 298,
                        ProductModelID = 7,
                        Color = "Red"
                    });
                    products.Add(new Product()
                    {
                        Name = "Borehole Diameter",
                        ProductNumber = "11111111111",
                        ListPrice = 298,
                        ProductModelID = 7,
                        Color = "Red"
                    });
               
            return products;
        }

        // Gets products bellow a specified max price.
        [DataObjectMethod(DataObjectMethodType.Select)]
        public IList<Product> GetProducts(int productModelID, string color)
        {
            return this.GetAllProducts();
        } 
    } and here is my xaml file

<Window x:Class="Telerik.Reproting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Height="500" Width="683" 
        xmlns:telerik="clr-namespace:Telerik.ReportViewer.Wpf;assembly=Telerik.ReportViewer.Wpf" 
        xmlns:telerikReporting="clr-namespace:Telerik.Reporting;assembly=Telerik.Reporting" Loaded="Window_Loaded">
    <Grid>
        <telerik:ReportViewer Name="reportViewer1" Margin="-10,26,9.8,0" VerticalAlignment="Top" Height="355"/>
    </Grid>
</Window>
Thanks







0
Stef
Telerik team
answered on 01 Aug 2014, 03:11 PM
Hello Muhammad,

A report document is actually a template you can design using our Report Designers, which template is repeated for each record in the assigned to it data. There is no auto-generate feature, so you should have created previously a report definition using fields existing in the used as data source object.

More information about starting with Telerik Reporting can be found in our Quickstart help section, the videos accompanying our online demos, and the local examples installed by default under C:\Program Files (x86)\Telerik\Reporting <VERSION>\Examples\CSharp. There you can find an example with each of the available report viewers.


The attached WPF project illustrated a report bound at run-time to the data in your model.

Regards,
Stef
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Muhammad
Top achievements
Rank 1
answered on 01 Aug 2014, 03:28 PM
Hi Stef,
Thanks it is working now.
Muhammad
0
Mark
Top achievements
Rank 1
answered on 06 Jul 2015, 06:22 PM

 Your example of:

<telerik:ReportViewer x:Name="ReportViewer1" ReportSource="{Binding MyReportSource}" />

Does not work..  The following error is encountered:  

A 'Binding' cannot be set on the 'Report' property of type 'ReportViewer'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.   Howerver we are using "ReportSource" not "Report".

 This is a major issue for us as we have spent days trying to use your examples to no avail.  We upgraded and now cannot use any of our old reports.  

0
Stef
Telerik team
answered on 09 Jul 2015, 01:34 PM
Hi Mark,

Please use the demo project from the Reporting WPF MVVM Sample forum thread. The project must be upgraded locally as it uses older version of Telerik Reporting. Note that Telerik Reporting is synchronized with the official releases of Telerik UI for WPF, and you must upgrade both products - for more details about dependencies check the Upgrade Path articles.

In general, the WPF ReportViewer ReportSource property can be bound to a model's property which is of type ReportSource. Report properties are not dependency properties and must be handled in code.
The error in your case points that you are binding the viewer's deprecated Report property, instead of the viewer's ReportSource property. Thus please double-check all views and models settings to verify the viewer's Report property is not set elsewhere in code.


If you need further help, please use the support ticketing system to sen  us more details about the problem.

Regards,
Stef
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Stefan Kamphuis
Top achievements
Rank 2
Answers by
Svetoslav
Telerik team
Gregory
Top achievements
Rank 1
Chavdar
Telerik team
William
Top achievements
Rank 1
IvanY
Telerik team
Muhammad
Top achievements
Rank 1
Stef
Telerik team
Mark
Top achievements
Rank 1
Share this question
or