方法: プロジェクト データ ソースを使用してデータをバインドする (WCF Data Services)

生成されたデータ オブジェクトに基づいて WCF Data Services クライアント アプリケーション内にデータ ソースを作成できます。 [サービス参照の追加] ダイアログを使用して参照をデータ サービスに追加すると、生成されたクライアント データ クラスと一緒にプロジェクト データ ソースが作成されます。 データ サービスが公開する各エンティティ セットに対して 1 つのデータ ソースが作成されます。 [データ ソース] ウィンドウからデータ ソースの項目をデザイナーにドラッグして、サービスのデータを表示するフォームを作成できます。 これらの項目は、データ ソースにバインドされているコントロールになります。 実行中、このデータ ソースは DataServiceCollection クラスのインスタンスにバインドされます。このインスタンスには、クエリによってデータ サービスに返されるオブジェクトが挿入されます。 詳細については、「コントロールへのデータのバインド (WCF Data Services)」を参照してください。

このトピックの例では、Northwind サンプル データ サービスおよび自動生成されたクライアント データ サービス クラスを使用します。 このサービスおよびクライアント データ クラスは、WCF Data Services クイック スタートを完了したときに作成されます。

WPF ウィンドウでプロジェクト データ ソースを使用するには

  1. WPF プロジェクトで Northwind データ サービスへの参照を追加します。 詳細については、「方法: データ サービス参照を追加する (WCF Data Services)」を参照してください。

  2. [データ ソース] ウィンドウで [NorthwindEntities] プロジェクト データ ソースの [Customers] ノードを展開します。

  3. [CustomerID] 項目をクリックし、一覧から [ComboBox] を選択して [CustomerID] 項目を [Customers] ノードからデザイナーにドラッグします。

    ウィンドウの XAML ファイルに次のオブジェクト要素が作成されます。

    • customersViewSource という名前の CollectionViewSource 要素。 最上位レベルの Grid オブジェクト要素の DataContext プロパティが、この新しい CollectionViewSource に設定されます。

    • CustomerID という名前のデータ バインド ComboBox

    • Label

  4. [Orders] ナビゲーション プロパティをデザイナーにドラッグします。

    ウィンドウの XAML ファイルに次の追加オブジェクト要素が作成されます。

    • customersOrdersViewSource という名前の 2 番目の CollectionViewSource 要素。このソースは customerViewSource です。

    • ordersDataGrid という名前のデータ バインド DataGrid コントロール。

  5. (省略可能) 追加の項目を [Customers] ノードからデザイナーにドラッグします。

  6. フォームのコード ページを開き、次に示す using ステートメント (Visual Basic の場合は Imports) を追加します。

    using System.Data.Services.Client;
    using NorthwindClient.Northwind;
    
  7. フォームを定義する部分クラスで、ObjectContext インスタンスを作成し、customerID の定数を定義する次のコードを追加します。

    Private context As NorthwindEntities
    Private customersViewSource As CollectionViewSource
    Private trackedCustomers As DataServiceCollection(Of Customer)
    Private Const customerCountry As String = "Germany"
    Private Const svcUri As String = "https://localhost:12345/Northwind.svc/"
    
    private NorthwindEntities context;
    private CollectionViewSource customersViewSource;
    private DataServiceCollection<Customer> trackedCustomers;
    private const string customerCountry = "Germany";
    private const string svcUri = "https://localhost:12345/Northwind.svc/";
    
  8. デザイナーでウィンドウを選択します。

    Ee373840.note(ja-jp,VS.100).gif注 :
    ウィンドウ内にある内容を選択するのではなく、ウィンドウ自身を選択します。ウィンドウを選択すると、[プロパティ] ウィンドウの上部近くの [名前] テキスト ボックスにウィンドウ名が表示されます。

  9. [プロパティ] ウィンドウで、[イベント] ボタンをクリックします。

  10. [Loaded] イベントを見つけて、このイベントの横にあるドロップダウン リストをダブルクリックします。

    Visual Studio でウィンドウの分離コード ファイルが開き、Loaded イベント ハンドラーが生成されます。

  11. 新しく作成された Loaded イベント ハンドラーで、次のコードをコピーして貼り付けます。

    ' Initialize the context for the data service.
    context = New NorthwindEntities(New Uri(svcUri))
    
    ' Create a LINQ query that returns customers with related orders.
    Dim customerQuery = From cust In context.Customers.Expand("Orders") _
                             Where cust.Country = customerCountry _
                             Select cust
    
    ' Create a new collection for binding based on the LINQ query.
    trackedCustomers = New DataServiceCollection(Of Customer)(customerQuery)
    
        try
            ' Get the customersViewSource resource and set the binding to the collection.
        customersViewSource = _
            CType(Me.FindResource("customersViewSource"), CollectionViewSource)
        customersViewSource.Source = trackedCustomers
        customersViewSource.View.MoveCurrentToFirst()
    Catch ex As DataServiceQueryException
        MessageBox.Show("The query could not be completed:\n" + ex.ToString())
    Catch ex As InvalidOperationException
        MessageBox.Show("The following error occurred:\n" + ex.ToString())
    End Try
    
    // Initialize the context for the data service.
    context = new NorthwindEntities(new Uri(svcUri));
    
    // Create a LINQ query that returns customers with related orders.
    var  customerQuery = from cust in context.Customers.Expand("Orders")
                         where cust.Country == customerCountry
                         select cust;
    
    // Create a new collection for binding based on the LINQ query.
    trackedCustomers = new DataServiceCollection<Customer>(customerQuery);
    
    try
    {
        // Get the customersViewSource resource and set the binding to the collection.
        customersViewSource =
            ((CollectionViewSource)(this.FindResource("customersViewSource")));
        customersViewSource.Source = trackedCustomers;
        customersViewSource.View.MoveCurrentToFirst();
    }
    catch (DataServiceQueryException ex)
    {
        MessageBox.Show("The query could not be completed:\n" + ex.ToString());
    }
    catch (InvalidOperationException ex)
    {
        MessageBox.Show("The following error occurred:\n" + ex.ToString());
    }
    
  12. このコードは、関連する Orders オブジェクトと一緒に CustomersIEnumerable を Northwind データ サービスから返す LINQ クエリの実行に基づいて Customers 型の DataServiceCollection のインスタンスを作成し、customersViewSource にバインドします。

Windows フォームでプロジェクト データ ソースを使用するには

  1. [データ ソース] ウィンドウで [NorthwindEntities] プロジェクト データ ソースの [Customers] ノードを展開します。

  2. [CustomerID] 項目をクリックし、一覧から [ComboBox] を選択して [CustomerID] 項目を [Customers] ノードからデザイナーにドラッグします。

    次のコントロールがフォーム上に作成されます。

    • customersBindingSource という名前の BindingSource のインスタンス。

    • customersBindingNavigator という名前の BindingNavigator のインスタンス。 このコントロールは必要ないので削除できます。

    • CustomerID という名前のデータ バインド ComboBox

    • Label

  3. [Orders] ナビゲーション プロパティをフォームにドラッグします。

  4. これにより、DataSource プロパティが customersBindingSource に設定され、DataMember プロパティが Customers に設定された ordersBindingSource コントロールが作成されます。 また、ordersDataGridView データ バインド コントロールも、適切なタイトルのラベル コントロールと共にフォーム上に作成されます。

  5. (省略可能) 追加の項目を [Customers] ノードからデザイナーにドラッグします。

  6. フォームのコード ページを開き、次に示す using ステートメント (Visual Basic の場合は Imports) を追加します。

    Imports System.Data.Services.Client
    Imports NorthwindClient.Northwind
    
    using System.Data.Services.Client;
    using NorthwindClient.Northwind;
    
  7. フォームを定義する部分クラスで、ObjectContext インスタンスを作成し、customerID の定数を定義する次のコードを追加します。

    Private context As NorthwindEntities
    Private trackedCustomers As DataServiceCollection(Of Customer)
    Private Const customerCountry As String = "Germany"
    Private Const svcUri As String = "http:'localhost:12345/Northwind.svc/"
    
    private NorthwindEntities context;
    private DataServiceCollection<Customer> trackedCustomers;
    private const string customerCountry = "Germany";
    private const string svcUri = "https://localhost:12345/Northwind.svc/";
    
  8. フォーム デザイナーで、フォームをダブルクリックします。

    フォームのコード ページが開き、フォームの Load イベントを処理するメソッドが作成されます。

  9. Load イベント ハンドラーで、次のコードをコピーして貼り付けます。

    ' Initialize the context for the data service.
    context = New NorthwindEntities(New Uri(svcUri))
    Try
        ' Create a LINQ query that returns customers with related orders.
        Dim customerQuery = From cust In context.Customers.Expand("Orders") _
                                Where cust.Country = customerCountry _
                                Select cust
    
        ' Create a new collection for binding based on the LINQ query.
        trackedCustomers = New DataServiceCollection(Of Customer)(customerQuery)
    
        'Bind the Customers combobox to the collection.
        customersComboBox.DisplayMember = "CustomerID"
        customersComboBox.DataSource = trackedCustomers
    Catch ex As DataServiceQueryException
        MessageBox.Show("The query could not be completed:\n" + ex.ToString())
    Catch ex As InvalidOperationException
        MessageBox.Show("The following error occurred:\n" + ex.ToString())
    
    // Initialize the context for the data service.
    context = new NorthwindEntities(new Uri(svcUri));
    try
    {
        // Create a LINQ query that returns customers with related orders.
        var customerQuery = from cust in context.Customers.Expand("Orders")
                            where cust.Country == customerCountry
                            select cust;
    
        // Create a new collection for binding based on the LINQ query.
        trackedCustomers = new DataServiceCollection<Customer>(customerQuery);
    
        //Bind the Customers combobox to the collection.
        customersComboBox.DisplayMember = "CustomerID";
        customersComboBox.DataSource = trackedCustomers;
    }
    catch (DataServiceQueryException ex)
    {
        MessageBox.Show("The query could not be completed:\n" + ex.ToString());
    }
    catch (InvalidOperationException ex)
    {
        MessageBox.Show("The following error occurred:\n" + ex.ToString());
    }
    
  10. このコードは、Northwind データ サービスから CustomersIEnumerable を返す DataServiceQuery の実行に基づいて Customers 型の DataServiceCollection のインスタンスを作成し、customersBindingSource にバインドします。

参照

処理手順

方法: Windows Presentation Foundation 要素にデータをバインドする (WCF Data Services)

その他のリソース

WCF Data Services クライアント ライブラリ