1 out of 1 rated this helpful - Rate this topic

ObjectDataProvider Class

Wraps and creates an object that you can use as a binding source.

System.Object
  System.Windows.Data.DataSourceProvider
    System.Windows.Data.ObjectDataProvider

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
public class ObjectDataProvider : DataSourceProvider
<ObjectDataProvider .../>

The ObjectDataProvider type exposes the following members.

  Name Description
Public method ObjectDataProvider Initializes a new instance of the ObjectDataProvider class.
Top
  Name Description
Public property ConstructorParameters Gets the list of parameters to pass to the constructor.
Public property Data Gets the underlying data object. (Inherited from DataSourceProvider.)
Protected property Dispatcher Gets or sets the current Dispatcher object to the UI thread to use. (Inherited from DataSourceProvider.)
Public property Error Gets the error of the last query operation. (Inherited from DataSourceProvider.)
Public property IsAsynchronous Gets or sets a value that indicates whether to perform object creation in a worker thread or in the active context.
Public property IsInitialLoadEnabled Gets or sets a value that indicates whether to prevent or delay the automatic loading of data. (Inherited from DataSourceProvider.)
Protected property IsRefreshDeferred Gets a value that indicates whether there is an outstanding DeferRefresh in use. (Inherited from DataSourceProvider.)
Public property MethodName Gets or sets the name of the method to call.
Public property MethodParameters Gets the list of parameters to pass to the method.
Public property ObjectInstance Gets or sets the object used as the binding source.
Public property ObjectType Gets or sets the type of object to create an instance of.
Top
  Name Description
Protected method BeginInit Indicates that initialization of this object is about to begin; no implicit Refresh occurs until the matched EndInit method is called. (Inherited from DataSourceProvider.)
Protected method BeginQuery Starts to create the requested object, either immediately or on a background thread, based on the value of the IsAsynchronous property. (Overrides DataSourceProvider.BeginQuery().)
Public method DeferRefresh Enters a defer cycle that you can use to change properties of the provider and delay automatic refresh. (Inherited from DataSourceProvider.)
Protected method EndInit Indicates that the initialization of this object has completed; this causes a Refresh if no other DeferRefresh is outstanding. (Inherited from DataSourceProvider.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InitialLoad Starts the initial query to the underlying data model. The result is returned on the Data property. (Inherited from DataSourceProvider.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnPropertyChanged Raises the PropertyChanged event with the provided arguments. (Inherited from DataSourceProvider.)
Protected method OnQueryFinished(Object) Derived classes call this method to indicate that a query has finished. (Inherited from DataSourceProvider.)
Protected method OnQueryFinished(Object, Exception, DispatcherOperationCallback, Object) Derived classes call this method to indicate that a query has finished. (Inherited from DataSourceProvider.)
Public method Refresh Initiates a refresh operation to the underlying data model. The result is returned on the Data property. (Inherited from DataSourceProvider.)
Public method ShouldSerializeConstructorParameters Indicates whether the ConstructorParameters property should be persisted.
Public method ShouldSerializeMethodParameters Indicates whether the MethodParameters property should be persisted.
Public method ShouldSerializeObjectInstance Indicates whether the ObjectInstance property should be persisted.
Public method ShouldSerializeObjectType Indicates whether the ObjectType property should be persisted.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public event DataChanged Occurs when the Data property has a new value. (Inherited from DataSourceProvider.)
Protected event PropertyChanged Occurs when a property value changes. (Inherited from DataSourceProvider.)
Top
  Name Description
Explicit interface implemetation Private event INotifyPropertyChanged.PropertyChanged Occurs when a property value changes. (Inherited from DataSourceProvider.)
Explicit interface implemetation Private method ISupportInitialize.BeginInit This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. (Inherited from DataSourceProvider.)
Explicit interface implemetation Private method ISupportInitialize.EndInit This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. (Inherited from DataSourceProvider.)
Top
Security note Security Note

ObjectDataProvider fails when it does not have permissions to perform reflection on the given type or member. For more information, see Permissions Requirements in Binding Sources Overview.

There are many ways to create an object to use as a binding source. For example, you can create your object in the resources section of your Extensible Application Markup Language (XAML) page, or you can create your object in code and set it as the DataContext of your window.

ObjectDataProvider enables you to create your object in XAML and make it available as a binding source. It provides the following properties that enable you to execute a query on your object and bind to the results.

  • Use the ConstructorParameters property to pass parameters to the constructor of your object.

  • Use the MethodName property to call a method and use the MethodParameters property to pass parameters to the method. You can then bind to the results of the method.

You can also use the IsAsynchronous property to specify whether to perform object creation in a worker thread or in the active context.

This class is also useful when you want to replace your current binding source object with another object and have all the associated bindings updated.

ObjectDataProvider provides a convenient way to create and use objects as binding source objects in XAML, but it does not replace existing data models.

If you are implementing your own objects for data binding, see Binding Sources Overview for information and recommendations.

This topic discusses the different ways you can make data available for binding in Extensible Application Markup Language (XAML), depending on the needs of your application.

If you have a common language runtime (CLR) object you would like to bind to from XAML, one way you can make the object available for binding is to define it as a resource and give it an x:Key. In the following example, you have a Person object with a string property named PersonName. The Person object is defined in the namespace called SDKSample.


<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:src="clr-namespace:SDKSample"
  SizeToContent="WidthAndHeight"
  Title="Simple Data Binding Sample">

  <Window.Resources>
    <src:Person x:Key="myDataSource" PersonName="Joe"/>


...


</Window.Resources>


You can then bind to the object in XAML, as shown in the following example.


<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>


Alternatively, you can use the ObjectDataProvider class, as in the following example.


<ObjectDataProvider x:Key="myDataSource" ObjectType="{x:Type src:Person}">
  <ObjectDataProvider.ConstructorParameters>
    <system:String>Joe</system:String>
  </ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>


You define the binding the same way:


<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>


In this particular example, the result is the same: you have a TextBlock with the text content Joe. However, the ObjectDataProvider class provides functionality such as the ability to bind to the result of a method. You can choose to use the ObjectDataProvider class if you need the functionality it provides.

However, if you are binding to an object that has already been created, you need to set the DataContext in code, as in the following example.


DataSet myDataSet;

private void OnInit(object sender, EventArgs e)
{
  string mdbFile = Path.Combine(AppDataPath, "BookData.mdb");
  string connString = string.Format(
      "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile);
  OleDbConnection conn = new OleDbConnection(connString);
  OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM BookTable;", conn);

  myDataSet = new DataSet();
  adapter.Fill(myDataSet, "BookTable");

  // myListBox is a ListBox control.
  // Set the DataContext of the ListBox to myDataSet
  myListBox.DataContext = myDataSet;
}


To access XML data for binding using the XmlDataProvider class, see How to: Bind to XML Data Using an XMLDataProvider and XPath Queries. To access XML data for binding using the ObjectDataProvider class, see How to: Bind to XDocument, XElement, or LINQ for XML Query Results.

For information about the different ways you can specify the data you are binding to, see How to: Specify the Binding Source. For information about what types of data you can bind to or how to implement your own common language runtime (CLR) objects for binding, see Binding Sources Overview.

More Code

How to: Bind to a Method The following example shows how to bind to a method using ObjectDataProvider.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Is using the ObjectDataProvider Class more expensive?
Hi,

Most scenarios in my application can be satisfied binding directly to my custom CLR object without need for the ObjectDataProvider. However, there are a couple of scenarios which would require me binding to a method; hence, I'd need the ObjectDatProvider. For consistency, I'd like to use one form and stick with it throughout which would be using the ObjectDataProvider in every scenario.

Will this approach cost me in terms of performance? Meaning, is there significant overhead to using the ObjectDataProvider vs. referencing CLR objects directly?

Thanks,
KBW

[tfl - 22 01 12] Hi - and thanks for your post. Community content is not the appropriate place for technical support queries. Instead,
you should visit the Technet Forums at http://forums.microsoft.com/technet, where such posts are welcomed and where you stand a much
better chance of getting your query resolved. Sorry if that's not the answer you wanted to hear.