DataServiceCollection(Of T) Class
Represents a dynamic entity collection that provides notifications when items get added, removed, or when the list is refreshed.
Assembly: System.Data.Services.Client (in System.Data.Services.Client.dll)
This topic describes new functionality in ADO.NET Data Services that is available as an update to the .NET Framework version 3.5 Service Pack 1. You can download and install the update from the Microsoft Download Center.
ADO.NET Data Services provides the DataServiceCollection(Of T) class to support binding data to controls in client applications. This class inherits from the ObservableCollection(Of T) class, which implements the INotifyCollectionChanged interface and is the primary data binding mechanism for Windows Presentation Foundation (WPF) and Silverlight-based applications.
You can load an ObservableCollection(Of T) binding collection by using any collection that implements the IEnumerable(Of T) interface. Items loaded into the binding collection must implement the INotifyPropertyChanged interface. For more information, see Binding Data to Controls (ADO.NET Data Services).
The following example is from the code-behind page for an Extensible Application Markup Language (XAML) page that defines the SalesOrders window in WPF. When the window is loaded, an DataServiceCollection(Of T) is created based on the result of a query that returns customers with related objects, filtered by country. This result is bound to the DataContext property of the StackPanel that is the root layout control for the WPF window.
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Data Imports System.Windows.Documents Imports System.Windows.Input Imports System.Windows.Media Imports System.Windows.Media.Imaging Imports System.Windows.Navigation Imports System.Windows.Shapes Imports System.Data.Services.Client Imports NorthwindClient.NorthwindModel Partial Public Class CustomerOrdersWpf Inherits Window Private context As NorthwindEntities Private trackedCustomers As DataServiceCollection(Of Customers) Private Const customerCountry As String = "Germany" Private Const svcUri As String = "http://localhost:12345/Northwind.svc/" Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Try ' 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 Customers)(customerQuery) ' Bind the root StackPanel element to the collection ' related object binding paths are defined in the XAML. Me.LayoutRoot.DataContext = 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 occured:\n" + ex.ToString()) End Try End Sub Private Sub saveChangesButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) ' Save changes to the data service. context.SaveChanges() End Sub End Class
The following is the XAML that defines the SalesOrders window in WPF for the previous example.
<Window x:Class="NorthwindClient.SalesOrders" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Northwind Orders" Height="335" Width="425" Name="OrdersForm" Loaded="OrdersForm_Loaded"> <Grid Name="orderItemsGrid"> <ComboBox DisplayMemberPath="SalesOrderID" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="true" Height="23" Margin="122,12,198,0" Name="comboBoxOrder" VerticalAlignment="Top"/> <ListView ItemsSource="{Binding Path=SalesOrderDetail}" Name="listViewItems" Margin="34,46,34,50"> <ListView.View> <GridView AllowsColumnReorder="False" ColumnHeaderToolTip="Line Items"> <GridViewColumn DisplayMemberBinding="{Binding Path=ProductID}" Header="Product" Width="50"/> <GridViewColumn DisplayMemberBinding="{Binding Path=OrderQty}" Header="Quantity" Width="50"/> <GridViewColumn DisplayMemberBinding="{Binding Path=UnitPrice}" Header="Cost" Width="50"/> <GridViewColumn DisplayMemberBinding="{Binding Path=LineTotal}" Header="Line Total" Width="80"/> </GridView> </ListView.View> </ListView> <Label Height="28" Margin="34,12,0,0" Name="orderLabel" VerticalAlignment="Top" HorizontalAlignment="Left" Width="93">Order:</Label> <StackPanel Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Right" > <Button Height="23" Margin="0,0,12,12" Name="buttonClose" VerticalAlignment="Bottom" Width="75" Click="buttonClose_Click">Close</Button> <Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="buttonSave" VerticalAlignment="Bottom" Width="75" Click="buttonSaveChanges_Click">Save Changes</Button> </StackPanel> </Grid> </Window>
<Window x:Class="NorthwindClient.CustomerOrdersWpf" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="423" Width="679" Loaded="Window_Loaded"> <StackPanel Orientation="Vertical" Height="Auto" Name="LayoutRoot" Width="Auto"> <Label Content="Customer ID" Margin="20,0,0,0" /> <ComboBox Name="customerIDComboBox" DisplayMemberPath="CustomerID" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" SelectedIndex="0" Height="23" Width="120" HorizontalAlignment="Left" Margin="20,0,0,0" VerticalAlignment="Center" /> <ListView ItemsSource="{Binding Path=Orders}" Name="ordersDataGrid" Margin="34,46,34,50"> <ListView.View> <GridView AllowsColumnReorder="False" ColumnHeaderToolTip="Line Items"> <GridViewColumn DisplayMemberBinding="{Binding Path=OrderID}" Header="Order ID" Width="50"/> <GridViewColumn DisplayMemberBinding="{Binding Path=OrderDate}" Header="Order Date" Width="50"/> <GridViewColumn DisplayMemberBinding="{Binding Path=Freight}" Header="Freight Cost" Width="50"/> </GridView> </ListView.View> </ListView> <Button Name="saveChangesButton" Content="Save Changes" Click="saveChangesButton_Click" Width="80" Height="30" Margin="450,0,0,0"/> </StackPanel> </Window>
System.Collections.ObjectModel.Collection(Of T)
System.Collections.ObjectModel.ObservableCollection(Of T)
System.Data.Services.Client.DataServiceCollection(Of T)
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.