2016-05-20 20 views
0

MVVM LightでWPF MVVMスタイルで実行されたプログラムを継承しました。 SQLサーバーからViewModelsに引き込まれたデータからプログラムから出てくるレポートが必要です。私はMVVMに固執することを心配していませんが、私はそれを動作させる必要があるだけです。WPF with report viewerバインディングの問題

私はViewModelのうちの1つを既にレポート用に自分のデータとして作成しようとしています。これまでのところ、私は、メインのボタンのクリックで新しいウィンドウが開きますDemoFormと呼ばれる形式を持つDATASET1にデータセット

をCalibrationViewModelデータソースを使用しているReport1.rdlcという名前のレポートを作成しましたWPFページにレポートビューア(reportViewer1)があります。これは、レポートを表示するために使用されます。 reportViewerはCalibrationViewModelBindingSourceにバインドされています。私はデータバインディングを逃すどこイムのように感じる

this.reportViewer1.RefreshReport(); 

を持ってDemoForm_Loadで

。私は、レポートをプルしようとすると、私はrdlc上のテーブルに移入しようとした3つのフィールドのヘッダーがあります。私は新しいフォームを開くときに、レポートのデータソースを設定する必要があると思います。

答えて

0

ここは簡単な例です。

XAML

<Window x:Class="WpfApplication55.MainWindow" 
     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:WpfApplication55" 
     xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <WindowsFormsHost> 
      <rv:ReportViewer x:Name="reportViewer1" /> 
     </WindowsFormsHost> 
    </Grid> 
</Window> 

CS

using System.Collections.Generic; 
using System.Windows; 

namespace WpfApplication55 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      reportViewer1.LocalReport.ReportEmbeddedResource = "WpfApplication55.Report1.rdlc"; 

      Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource(); 
      reportDataSource1.Name = "DataSet1"; 
      reportDataSource1.Value = new List<MyReportData>() { new MyReportData() { Field1 = "Value 1", Field2 = "Value 2" } }; 
      reportViewer1.LocalReport.DataSources.Add(reportDataSource1); 
      reportViewer1.RefreshReport(); 
     } 
    } 

    public class MyReportData 
    { 
     public string Field1 { get; set; } 
     public string Field2 { get; set; } 
    } 
} 

RDLC(スクリーンショット) enter image description here

ランタイムスクリーンショット enter image description here

+0

私はのForm_Loadに、この部分を追加したが、何のわかりませんよ置くere - reportDataSource1.Value = dataset;/*ここでデータソースを使用する* /私のソースはviewModelオブジェクトです –

+0

レポートでは、特定のタイプのデータセット "DataSet1"を設定しました。値をその型のインスタンスに設定します。質問で "CalibrationViewModel"と言いましたので、そのインスタンスになると思います。 –

+0

CalibrationViewModel1を値として使用すると、現在のコンテキストに存在しないというエラーが表示されます。これは私がform_loadに入れたものです。 Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource(); reportDataSource1.Name = "DataSet2"; reportDataSource1.Value = CalibrationViewModel1; reportViewer1.LocalReport.DataSources.Add(reportDataSource1); reportViewer1.RefreshReport(); –

関連する問題