Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ObjectDataProvider Class

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

Namespace: System.Windows.Data
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)] 
public class ObjectDataProvider : DataSourceProvider
/** @attribute LocalizabilityAttribute(LocalizationCategory.NeverLocalize) */ 
public class ObjectDataProvider extends DataSourceProvider
LocalizabilityAttribute(LocalizationCategory.NeverLocalize) 
public class ObjectDataProvider extends DataSourceProvider
<ObjectDataProvider .../>
Security noteSecurity Note:

ObjectDataProvider fails when it does not have permissions to perform reflection on the given type or member.

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 to be available as a binding source and provides the following properties for 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 is 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 see the complete samples, see Simple Binding Sample and Binding with Data in an ADO DataSet Sample.

To access XML data for binding, use the XmlDataProvider class. For an example, see How to: Bind to XML Data Using an XMLDataProvider and XPath Queries.

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.

System.Object
   System.Windows.Data.DataSourceProvider
    System.Windows.Data.ObjectDataProvider
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.