Export (0) Print
Expand All

DataServiceCollection<T> Class

Represents a dynamic entity collection that provides notifications when items get added, removed, or when the list is refreshed.

Namespace:  System.Data.Services.Client
Assembly:  Microsoft.Data.Services.Client (in Microsoft.Data.Services.Client.dll)

public class DataServiceCollection<T> : ObservableCollection<T>

Type Parameters

T

An entity type.

The DataServiceCollection<T> type exposes the following members.

  NameDescription
Public methodDataServiceCollection<T>()Initializes a new instance of the DataServiceCollection<T> class.
Public methodDataServiceCollection<T>(IEnumerable<T>)Initializes a new instance of the DataServiceCollection<T> class based on query execution.
Public methodDataServiceCollection<T>(DataServiceContext)Initializes a new instance of the DataServiceCollection<T> class that uses the specified DataServiceContext.
Public methodDataServiceCollection<T>(IEnumerable<T>, TrackingMode)Initializes a new instance of the DataServiceCollection<T> class based on query execution and with the specified tracking mode.
Public methodDataServiceCollection<T>(DataServiceContext, String, Func<EntityChangedParams, Boolean>, Func<EntityCollectionChangedParams, Boolean>)Initializes a new instance of the DataServiceCollection<T> class with the supplied change method delegates and that uses the specified DataServiceContext.
Public methodDataServiceCollection<T>(IEnumerable<T>, TrackingMode, String, Func<EntityChangedParams, Boolean>, Func<EntityCollectionChangedParams, Boolean>)Initializes a new instance of the DataServiceCollection<T> class based on query execution and with the supplied change method delegates.
Public methodDataServiceCollection<T>(DataServiceContext, IEnumerable<T>, TrackingMode, String, Func<EntityChangedParams, Boolean>, Func<EntityCollectionChangedParams, Boolean>)Initializes a new instance of the DataServiceCollection<T> class based on query execution, with the supplied change method delegates, and that uses the supplied DataServiceContext.
Top

  NameDescription
Public propertyContinuationGets a continuation object that is used to return the next set of paged results.
Public propertyCount (Inherited from Collection<T>.)
Public propertyItem (Inherited from Collection<T>.)
Protected propertyItems (Inherited from Collection<T>.)
Top

  NameDescription
Public methodAdd (Inherited from Collection<T>.)
Protected methodBlockReentrancy (Inherited from ObservableCollection<T>.)
Protected methodCheckReentrancy (Inherited from ObservableCollection<T>.)
Public methodClear() (Inherited from Collection<T>.)
Public methodClear(Boolean)Indicates whether all the items from the collection are removed.
Protected methodClearItems (Inherited from ObservableCollection<T>.)
Public methodContains (Inherited from Collection<T>.)
Public methodCopyTo (Inherited from Collection<T>.)
Public methodDetachDisables the DataServiceContext tracking of all items in the collection.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetEnumerator (Inherited from Collection<T>.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodIndexOf (Inherited from Collection<T>.)
Public methodInsert (Inherited from Collection<T>.)
Protected methodInsertItemAdds a specified item to the collection at the specified index. (Overrides ObservableCollection<T>.InsertItem(Int32, T).)
Public methodLoad(IEnumerable<T>)Loads a collection of entity objects into the collection.
Public methodLoad(T)Loads a single entity object into the collection.
Protected methodMemberwiseClone (Inherited from Object.)
Public methodMove (Inherited from ObservableCollection<T>.)
Protected methodMoveItem (Inherited from ObservableCollection<T>.)
Protected methodOnCollectionChanged (Inherited from ObservableCollection<T>.)
Protected methodOnPropertyChanged (Inherited from ObservableCollection<T>.)
Public methodRemove (Inherited from Collection<T>.)
Public methodRemoveAt (Inherited from Collection<T>.)
Protected methodRemoveItem (Inherited from ObservableCollection<T>.)
Protected methodSetItem (Inherited from ObservableCollection<T>.)
Public methodToString (Inherited from Object.)
Top

  NameDescription
Public eventCollectionChanged (Inherited from ObservableCollection<T>.)
Protected eventPropertyChanged (Inherited from ObservableCollection<T>.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIList.Add (Inherited from Collection<T>.)
Explicit interface implemetationPrivate methodIList.Contains (Inherited from Collection<T>.)
Explicit interface implemetationPrivate methodICollection.CopyTo (Inherited from Collection<T>.)
Explicit interface implemetationPrivate methodIEnumerable.GetEnumerator (Inherited from Collection<T>.)
Explicit interface implemetationPrivate methodIList.IndexOf (Inherited from Collection<T>.)
Explicit interface implemetationPrivate methodIList.Insert (Inherited from Collection<T>.)
Explicit interface implemetationPrivate propertyIList.IsFixedSize (Inherited from Collection<T>.)
Explicit interface implemetationPrivate propertyICollection<T>.IsReadOnly (Inherited from Collection<T>.)
Explicit interface implemetationPrivate propertyIList.IsReadOnly (Inherited from Collection<T>.)
Explicit interface implemetationPrivate propertyICollection.IsSynchronized (Inherited from Collection<T>.)
Explicit interface implemetationPrivate propertyIList.Item (Inherited from Collection<T>.)
Explicit interface implemetationPrivate eventINotifyPropertyChanged.PropertyChanged (Inherited from ObservableCollection<T>.)
Explicit interface implemetationPrivate methodIList.Remove (Inherited from Collection<T>.)
Explicit interface implemetationPrivate propertyICollection.SyncRoot (Inherited from Collection<T>.)
Top

WCF Data Services provides the DataServiceCollection<T> class to support binding data to controls in client applications. This class inherits from the ObservableCollection<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<T> binding collection by using any collection that implements the IEnumerable<T> interface. Items loaded into the binding collection must implement the INotifyPropertyChanged interface. For more information, see Binding Data to Controls (WCF 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<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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Services.Client;
using NorthwindClient.Northwind;

namespace NorthwindClient
{
    public partial class CustomerOrdersWpf : Window
    {
        private NorthwindEntities context;
        private DataServiceCollection<Customer> trackedCustomers;
        private const string customerCountry = "Germany";
        private const string svcUri = "http://localhost:12345/Northwind.svc/";

        public CustomerOrdersWpf()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            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);

                // Bind the root StackPanel element to the collection;
                // related object binding paths are defined in the XAML.
                LayoutRoot.DataContext = 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());
            }
        }
        private void saveChangesButton_Click(object sender, RoutedEventArgs e)
        {
            // Save changes to the data service.
            context.SaveChanges();
        }
    }
}

The following is the XAML that defines the SalesOrders window in WPF for the previous example.

    <Window x:Class="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, Mode=OneWay}" 
                        Header="Order ID" Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=OrderDate, Mode=TwoWay}" 
                        Header="Order Date" Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Freight, Mode=TwoWay}" 
                        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>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft