How to: Customize Data Binding Behaviors (WCF Data Services)


With WCF Data Services, you can supply custom logic that is called by the DataServiceCollection<T> when an object is added or removed from the binding collection or when a property change is detected. This custom logic is provided as methods, referenced as Func<T, TResult> delegates, that return a value of false when the default behavior should still be performed when the custom method completes and true when subsequent processing of the event should be stopped.

The examples in this topic supply custom methods for both the entityChanged and entityCollectionChanged parameters of DataServiceCollection<T>. The examples in this topic use the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the WCF Data Services quickstart.

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.

[!CODE [Astoria Northwind Client#WpfDataBindingCustom](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#wpfdatabindingcustom)]

The following XAML defines the window for the previous example.

[!CODE [Astoria Northwind Client#WpfDataBindingCustomXaml](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#wpfdatabindingcustomxaml)]

WCF Data Services Client Library