如何:使用项目数据源绑定数据(WCF 数据服务)
可以创建基于 WCF 数据服务 客户端应用程序中生成的数据对象的数据源。 当使用添加对数据服务的引用(通过添加服务引用对话框)时,会创建项目数据源并与生成的客户端数据类一起创建。一个数据源为数据服务公开的每个实体集创建。 您可以通过将这些数据源项从**“数据源”**窗口拖到设计器中来创建将显示服务中的数据的窗体。 这些项将成为绑定到该数据源的控件。 执行期间,此数据源绑定到 DataServiceCollection<T> 类的实例,该类填充有数据服务查询返回的对象。 有关更多信息,请参见将数据绑定到控件(WCF 数据服务)。
本主题中的示例使用 Northwind 示例数据服务和自动生成的客户端数据服务类。 此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。
在 WPF 窗口中使用项目数据源
在 WPF 项目中,添加对 Northwind 数据服务的引用。 有关更多信息,请参见如何:添加数据服务引用(WCF 数据服务)。
在**“数据源”窗口中,展开“NorthwindEntities”**项目数据源中的 Customers 节点。
单击**“CustomerID”项,在列表中选择“组合框”,然后将“CustomerID”项从“Customers”**节点拖到设计器中。
这会在 XAML 文件中为此窗口创建以下对象元素:
一个名为 customersViewSource 的 CollectionViewSource 元素。 将顶级 Grid 对象元素的 DataContext 属性设置为此新的 CollectionViewSource。
一个名为 CustomerID 的数据绑定 ComboBox。
一个 Label。
将**“订单”**导航属性拖到设计器中。
这会在 XAML 文件中为此窗口创建以下附加对象元素:
另一个名为 customersOrdersViewSource 的 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()); }
此代码根据 LINQ 查询(返回 Northwind 数据服务中 Customers 和相关 Orders 对象的 IEnumerable<T>)的执行情况为 Customers 类型创建一个 DataServiceCollection<T> 实例,并将其绑定到 customersViewSource。
在 Windows 窗体中使用项目数据源
在**“数据源”窗口中,展开“NorthwindEntities”项目数据源中的“Customers”**节点。
单击**“CustomerID”项,在列表中选择“组合框”,然后将“CustomerID”项从“Customers”**节点拖到设计器中。
这会在窗体上创建以下控件:
一个名为 customersBindingSource 的 BindingSource 实例。
一个名为 customersBindingNavigator 的 BindingNavigator 实例。 您可以删除此控件,因为它不是必需的。
一个名为 CustomerID 的数据绑定 ComboBox。
一个 Label。
将**“订单”**导航属性拖到窗体中。
这会创建 ordersBindingSource 控件,该控件的 DataSource 属性和 DataMember 属性分别设置为 customersBindingSource 和 Customers。 此外,还会在窗体上创建 ordersDataGridView 数据绑定控件,并附带经过相应命名的标签控件。
(可选)将附加项从**“Customers”**节点拖到设计器中。
打开窗体的代码页,并添加以下 using 语句(在 Visual Basic 中为 Imports):
Imports System.Data.Services.Client Imports NorthwindClient.Northwind
using 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()); }
此代码根据 DataServiceQuery<TElement>(返回 Northwind 数据服务中 Customers 的 IEnumerable<T>)的执行情况为 Customers 类型创建一个 DataServiceCollection<T> 实例,并将其绑定到 customersBindingSource。
请参阅
任务
如何:将数据绑定到 Windows Presentation Foundation 元素(WCF 数据服务)