c# - How to set Data Source In Crystal Report from View Model in MVVM pattern WPF Application -
below code setting document in view model
reportdocument report = new reportdocument(); report.load("reports/test.rpt"); var data_ = db.det_qualificationmaster.where(x => x.mua_id == 6) .select(x => new { x.collagename, x.startyear, x.endyear }).tolist(); report.setdatasource(data_);
code in page
<viewer:crystalreportsviewer horizontalalignment="left" name="crystalreportsviewer1" verticalalignment="top" height="400" width="400" >
how can bind data view model page
i found solution myself thought share all.big thank andre-alves lima blog here's link http://www.andrealveslima.com.br/blog/index.php/2016/07/20/utilizando-o-crystal-reports-com-mvvm-no-wpf/
it's in portuguese translate i'm putting code below
add static class reportsourcebehaviour.cs
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 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); } }
in viewmodel
public class mainviewmodel { public crystaldecisions.crystalreports.engine.reportdocument report { get; set; } public mainviewmodel() { report = new nameofrptfile(); //add data report } }
in page (xaml)
<window xmlns:viewer="clr-namespace:sapbusinessobjects.wpf.viewer;assembly=sapbusinessobjects.wpf.viewer" x:class="crystalwpfmvvm.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:crystalwpfmvvm" title="mainwindow" height="350" width="525" datacontext="{staticresource mainviewmodel}"> <grid> <viewer:crystalreportsviewer x:name="crystalreportsviewer" local:reportsourcebehaviour.reportsource="{binding path=datacontext.report, relativesource={relativesource mode=findancestor, ancestortype=frameworkelement}}"/> </grid> </window>
wiki
Comments
Post a Comment