生成されたデータ オブジェクトに基づいて WCF Data Services クライアント アプリケーション内にデータ ソースを作成できます。 [サービス参照の追加] ダイアログを使用して参照をデータ サービスに追加すると、生成されたクライアント データ クラスと一緒にプロジェクト データ ソースが作成されます。 データ サービスが公開する各エンティティ セットに対して 1 つのデータ ソースが作成されます。 [データ ソース] ウィンドウからデータ ソースの項目をデザイナーにドラッグして、サービスのデータを表示するフォームを作成できます。 これらの項目は、データ ソースにバインドされているコントロールになります。 実行中、このデータ ソースは DataServiceCollection クラスのインスタンスにバインドされます。このインスタンスには、クエリによってデータ サービスに返されるオブジェクトが挿入されます。 詳細については、「コントロールへのデータのバインド (WCF Data Services)」を参照してください。
このトピックの例では、Northwind サンプル データ サービスおよび自動生成されたクライアント データ サービス クラスを使用します。 このサービスおよびクライアント データ クラスは、WCF Data Services クイック スタートを完了したときに作成されます。
WPF ウィンドウでプロジェクト データ ソースを使用するには
WPF プロジェクトで Northwind データ サービスへの参照を追加します。 詳細については、「方法: データ サービス参照を追加する (WCF Data Services)」を参照してください。
[データ ソース] ウィンドウで [NorthwindEntities] プロジェクト データ ソースの [
Customers] ノードを展開します。[CustomerID] 項目をクリックし、一覧から [ComboBox] を選択して [CustomerID] 項目を [Customers] ノードからデザイナーにドラッグします。
ウィンドウの XAML ファイルに次のオブジェクト要素が作成されます。
customersViewSourceという名前の CollectionViewSource 要素。 最上位レベルの Grid オブジェクト要素の DataContext プロパティが、この新しい CollectionViewSource に設定されます。CustomerIDという名前のデータ バインド ComboBox。
[Orders] ナビゲーション プロパティをデザイナーにドラッグします。
ウィンドウの XAML ファイルに次の追加オブジェクト要素が作成されます。
customersOrdersViewSourceという名前の 2 番目の CollectionViewSource 要素。このソースはcustomerViewSourceです。ordersDataGridという名前のデータ バインド DataGrid コントロール。
(省略可能) 追加の項目を [Customers] ノードからデザイナーにドラッグします。
フォームのコード ページを開き、次に示す using ステートメント (Visual Basic の場合は Imports) を追加します。
using System.Data.Services.Client; using NorthwindClient.Northwind;フォームを定義する部分クラスで、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/";デザイナーでウィンドウを選択します。
注 :ウィンドウ内にある内容を選択するのではなく、ウィンドウ自身を選択します。ウィンドウを選択すると、[プロパティ] ウィンドウの上部近くの [名前] テキスト ボックスにウィンドウ名が表示されます。 [プロパティ] ウィンドウで、[イベント] ボタンをクリックします。
[Loaded] イベントを見つけて、このイベントの横にあるドロップダウン リストをダブルクリックします。
Visual Studio でウィンドウの分離コード ファイルが開き、Loaded イベント ハンドラーが生成されます。
新しく作成された 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()); }このコードは、関連する
Ordersオブジェクトと一緒にCustomersの IEnumerable を Northwind データ サービスから返す LINQ クエリの実行に基づいてCustomers型の DataServiceCollection のインスタンスを作成し、customersViewSourceにバインドします。
Windows フォームでプロジェクト データ ソースを使用するには
[データ ソース] ウィンドウで [NorthwindEntities] プロジェクト データ ソースの [Customers] ノードを展開します。
[CustomerID] 項目をクリックし、一覧から [ComboBox] を選択して [CustomerID] 項目を [Customers] ノードからデザイナーにドラッグします。
次のコントロールがフォーム上に作成されます。
customersBindingSourceという名前の BindingSource のインスタンス。customersBindingNavigatorという名前の BindingNavigator のインスタンス。 このコントロールは必要ないので削除できます。CustomerIDという名前のデータ バインド ComboBox。
[Orders] ナビゲーション プロパティをフォームにドラッグします。
これにより、DataSource プロパティが
customersBindingSourceに設定され、DataMember プロパティがCustomersに設定されたordersBindingSourceコントロールが作成されます。 また、ordersDataGridViewデータ バインド コントロールも、適切なタイトルのラベル コントロールと共にフォーム上に作成されます。(省略可能) 追加の項目を [Customers] ノードからデザイナーにドラッグします。
フォームのコード ページを開き、次に示す using ステートメント (Visual Basic の場合は Imports) を追加します。
Imports System.Data.Services.Client Imports NorthwindClient.Northwindusing System.Data.Services.Client; using NorthwindClient.Northwind;フォームを定義する部分クラスで、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/";フォーム デザイナーで、フォームをダブルクリックします。
フォームのコード ページが開き、フォームの
Loadイベントを処理するメソッドが作成されます。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()); }このコードは、Northwind データ サービスから
Customersの IEnumerable を返す DataServiceQuery の実行に基づいてCustomers型の DataServiceCollection のインスタンスを作成し、customersBindingSourceにバインドします。
参照
処理手順
方法: Windows Presentation Foundation 要素にデータをバインドする (WCF Data Services)