EntityCollectionChangedParams Class

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 ref class EntityCollectionChangedParams sealed

The EntityCollectionChangedParams type exposes the following members.

Public propertyActionA NotifyCollectionChangedAction value that indicates how the collection was changed.
Public propertyCollectionThe DataServiceCollection<T> that has changed.
Public propertyContextThe DataServiceContext associated with the DataServiceCollection<T> that has changed.
Public propertyPropertyNameThe navigation property on the source object that references the collection that has changed.
Public propertySourceEntityThe source object that references the target object in the collection by using a navigation property.
Public propertySourceEntitySetThe entity set of the source object.
Public propertyTargetEntityThe entity object in the collection that has changed.
Public propertyTargetEntitySetThe entity set name of the object in the collection.

Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns 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.

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.Northwind
Imports System.Collections.Specialized

Partial Public Class CustomerOrdersCustom
    Inherits Window
    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 Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' 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, _
                    TrackingMode.AutoChangeTracking, "Customers", _
                    AddressOf OnMyPropertyChanged, AddressOf OnMyCollectionChanged)

            ' 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 occurred:\n" + ex.ToString())
        End Try 
    End Sub 
    ' Method that is called when the CollectionChanged event is handled. 
    Private Function OnMyCollectionChanged( _
        ByVal entityCollectionChangedinfo As EntityCollectionChangedParams) As Boolean 
        If entityCollectionChangedinfo.Action = _
            NotifyCollectionChangedAction.Remove Then 

            ' Delete the related items when an order is deleted. 
            If entityCollectionChangedinfo.TargetEntity.GetType() Is GetType(Order) Then 

                ' Get the context and object from the supplied parameter. 
                Dim context = entityCollectionChangedinfo.Context
                Dim deletedOrder As Order = _
                CType(entityCollectionChangedinfo.TargetEntity, Order)

                If deletedOrder.Order_Details.Count = 0 Then 
                    ' Load the related OrderDetails.
                    context.LoadProperty(deletedOrder, "Order_Details")
                End If 

                ' Delete the order and its related items 
                For Each item As Order_Detail In deletedOrder.Order_Details

                ' Delete the order and then return false since the object is already deleted.

                Return True 
                Return False 
            End If 
            ' Use the default behavior. 
            Return False 
        End If 
    End Function 
    ' Method that is called when the PropertyChanged event is handled. 
    Private Function OnMyPropertyChanged( _
    ByVal entityChangedInfo As EntityChangedParams) As Boolean 
        ' Validate a changed order to ensure that changes are not made  
        ' after the order ships. 
        If entityChangedInfo.Entity.GetType() Is GetType(Order) AndAlso _
            (CType(entityChangedInfo.Entity, Order).ShippedDate < DateTime.Today) Then 
            Throw New ApplicationException(String.Format( _
                "The order {0} cannot be changed because it shipped on {1}.", _
                CType(entityChangedInfo.Entity, Order).OrderID, _
                CType(entityChangedInfo.Entity, Order).ShippedDate))
            Return False 
            Return True 
        End If 
    End Function 
    Private Sub deleteButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Get the Orders binding collection. 
        If customerIDComboBox.SelectedItem IsNot Nothing Then 
            Dim trackedOrders As DataServiceCollection(Of Order) = _
                (CType(customerIDComboBox.SelectedItem, Customer)).Orders

            ' Remove the currently selected order.
            trackedOrders.Remove(CType(ordersDataGrid.SelectedItem, Order))
        End If 
    End Sub 
    Private Sub saveChangesButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            ' Save changes to the data service.
        Catch ex As Exception
        End Try 
    End Sub 
End Class
No code example is currently available or this language may not be supported.

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

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

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