我试图在WPF中使用VS2010附带的报表查看器。我创建了报告并使用它运行的Windows应用程序进行了相同的测试。然后我使用相同的参数集在WPF中使用相同的RDLC文件,但它失败了。我正在使用的代码如下MainWindow.xaml.cs
public MainWindow()
{
ObjectModel DataObject = new ObjectModel();
DataObject.SetEPSDetails();
WindowsFormsHost host = new WindowsFormsHost();
ReportViewer RptViewer = new ReportViewer();
host.Child = RptViewer;
Grid HostGrid = this.FindName("GrdRow") as Grid;
HostGrid.Children.Add(host);
RptViewer.ProcessingMode = ProcessingMode.Local;
RptViewer.LocalReport.ReportEmbeddedResource = "RDLWPF.PrintPreview.rdlc";
RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("Payer", DataObject.Payer));
RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("ValueDate", DataObject.ValueDate));
RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("FileName", DataObject.FileName));
RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("Description", DataObject.Description));
RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("PrintedDate", DataObject.PrintDate));
RptViewer.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("FileLastUpdated", DataObject.FileLastUpdated));
RptViewer.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("FileDetails", DataObject.EPSDetails));
RptViewer.RefreshReport();
}
Xaml如下Mainwindow.Xaml
<Window x:Class="RDLWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewer="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
Title="MainWindow" Height="350" Width="525" AllowsTransparency="False" >
<Grid x:Name="GrdRow">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
</Grid>
我刚刚获得空的XAML窗口。报告控件也不会显示。我知道我犯了一个错误,却找不到它是什么。有谁可以为我纠正这个问题。此外,如果有人可以在MVVM appraoch中向我发送代码示例,那也很棒
谢谢和问候 卡塔斯
答案 0 :(得分:5)
以下是我在我的WPF应用程序中显示SQL Report Viewer所做的工作:
1-添加对WindowsFormsIntegation的项目引用
2-添加项目引用到Microsoft.ReportViewer.Winforms
3-将项目引用添加到Microsoft.ReportViewer.Common
4-在我的视图模型中定义WindowsFormsHost
public MainWindowViewModel()
{
ReportViewer reportViewer = new ReportViewer();
_viewer.Child = reportViewer;
}
private WindowsFormsHost _viewer = new WindowsFormsHost();
public WindowsFormsHost Viewer
{
get
{
return _viewer;
}
set
{
_viewer = value;
NotifyPropertyChanged("Viewer");
}
}
4-从视图中绑定对视图模型的引用
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0" Content="{Binding Viewer}" />
</Grid>