EntityCollectionChangedParams Class


Encapsulates the arguments of a CollectionChanged delegate.

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


public sealed class EntityCollectionChangedParams


A NotifyCollectionChangedAction value that indicates how the collection was changed.


The DataServiceCollection<T> that has changed.


The DataServiceContext associated with the DataServiceCollection<T> that has changed.


The navigation property on the source object that references the collection that has changed.


The source object that references the target object in the collection by using a navigation property.


The entity set of the source object.


The entity object in the collection that has changed.


The entity set name of the object in the collection.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

The following code-behind page for the XAML file creates a DataServiceCollection<T> with custom methods that are called when changes occur to data that is bound to the binding collection. When the CollectionChanged event occurs, the supplied method prevents an item that has been removed from the binding collection from being deleted from the data service. When the PropertyChanged event occurs, the ShipDate value is validated to ensure that changes are not made to orders that have already shipped.

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;
using System.Collections.Specialized;

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

        public CustomerOrdersCustom()

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

                // Bind the root StackPanel element to the collection;
                // related object binding paths are defined in the XAML.
                this.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());

        // Method that is called when the CollectionChanged event is handled.
        private bool OnCollectionChanged(
            EntityCollectionChangedParams entityCollectionChangedinfo)
            if (entityCollectionChangedinfo.Action ==
                // Delete the related items when an order is deleted.
                if (entityCollectionChangedinfo.TargetEntity.GetType() == typeof(Order))
                    // Get the context and object from the supplied parameter.
                    DataServiceContext context = entityCollectionChangedinfo.Context;
                    Order deletedOrder = entityCollectionChangedinfo.TargetEntity as Order;

                    if (deletedOrder.Order_Details.Count == 0)
                        // Load the related OrderDetails.
                        context.LoadProperty(deletedOrder, "Order_Details");

                    // Delete the order and its related items;
                    foreach (Order_Detail item in deletedOrder.Order_Details)

                    // Delete the order and then return true since the object is already deleted.

                    return true;
                    return false;
                // Use the default behavior.
                return false;

        // Method that is called when the PropertyChanged event is handled.
        private bool OnPropertyChanged(EntityChangedParams entityChangedInfo)
            // Validate a changed order to ensure that changes are not made 
            // after the order ships.
            if ((entityChangedInfo.Entity.GetType() == typeof(Order)) && 
                ((Order)(entityChangedInfo.Entity)).ShippedDate < DateTime.Today)
                throw new ApplicationException(string.Format(
                    "The order {0} cannot be changed because it shipped on {1}.",
            return false;

        private void deleteButton_Click(object sender, RoutedEventArgs e)
            if (customerIDComboBox.SelectedItem != null)
                // Get the Orders binding collection.
                DataServiceCollection<Order> trackedOrders =

                // Remove the currently selected order.

        private void saveChangesButton_Click(object sender, RoutedEventArgs e)
                // Save changes to the data service.
            catch (Exception ex)

The following XAML code defines the window for the previous example.

<Window x:Class="CustomerOrdersCustom"
             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">
                <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"/>
        <StackPanel Orientation="Horizontal">
            <Button Name="deleteButton" Content="Delete Order" Click="deleteButton_Click" 
                Width="80" Height="30" Margin="450,0,10,0"/>
            <Button Name="saveChangesButton" Content="Save Changes" Click="saveChangesButton_Click" 
                Width="80" Height="30" Margin="10,0,0,0"/>

.NET Framework
Available since 3.5
Available since 4.0

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

Return to top