This is ReportSourceBehaviour:
namespace Wpf_Mvvm_CrystalReport.Reports
{
public static class ReportSourceBehaviour
{
public static readonly System.Windows.DependencyProperty ReportSourceProperty =
System.Windows.DependencyProperty.RegisterAttached(
"ReportSource",
typeof(object),
typeof(ReportSourceBehaviour),
new System.Windows.PropertyMetadata(ReportSourceChanged));
private static void ReportSourceChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
{
var crviewer = d as SAPBusinessObjects.WPF.Viewer.CrystalReportsViewer;
if (crviewer != null)
{
crviewer.ViewerCore.ReportSource = e.NewValue;
}
}
public static void SetReportSource(System.Windows.DependencyObject target, object value)
{
target.SetValue(ReportSourceProperty, value);
}
public static object GetReportSource(System.Windows.DependencyObject target)
{
return target.GetValue(ReportSourceProperty);
}
}
}
This is MainWindow.xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf_Mvvm_CrystalReport.Views"
xmlns:Viewer="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer" x:Class="Wpf_Mvvm_CrystalReport.Views.MainWindow"
mc:Ignorable="d"
xmlns:dd="clr-namespace:Wpf_Mvvm_CrystalReport.ViewModels"
d:DataContext="{d:DesignInstance dd:MainWindowViewModel,IsDesignTimeCreatable=True}"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:rpt="clr-namespace:Wpf_Mvvm_CrystalReport.Reports"
Title="MainWindow" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<TextBlock Text="This is test report using crystal report with Mvvm Prism: "/>
<TextBlock Text="{Binding Flow}"/>
</StackPanel>
<Viewer:CrystalReportsViewer Grid.Row="1" rpt:ReportSourceBehaviour.ReportSource="{Binding Report}"/>
</Grid>
</Window>
This is MainWindowViewModel.cs:
using CrystalDecisions.CrystalReports.Engine;
using Prism.Mvvm;
using Wpf_Mvvm_CrystalReport.Reports;
namespace Wpf_Mvvm_CrystalReport.ViewModels
{
class MainWindowViewModel : BindableBase
{
public ReportDocument Report { get; set; }
private ushort _Flow = 2;
public ushort Flow
{
get { return _Flow; }
set { SetProperty(ref _Flow, value); }
}
public MainWindowViewModel()
{
Report = new CrystalReport1();
}
}
}