Export (0) Print
Expand All

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 ref class ObjectDataProvider : public DataSourceProvider
<ObjectDataProvider .../>

The ObjectDataProvider type exposes the following members.

  NameDescription
Public methodObjectDataProviderInitializes a new instance of the ObjectDataProvider class.
Top

  NameDescription
Public propertyConstructorParametersGets the list of parameters to pass to the constructor.
Public propertyDataGets the underlying data object. (Inherited from DataSourceProvider.)
Protected propertyDispatcherGets or sets the current Dispatcher object to the UI thread to use. (Inherited from DataSourceProvider.)
Public propertyErrorGets the error of the last query operation. (Inherited from DataSourceProvider.)
Public propertyIsAsynchronousGets or sets a value that indicates whether to perform object creation in a worker thread or in the active context.
Public propertyIsInitialLoadEnabledGets or sets a value that indicates whether to prevent or delay the automatic loading of data. (Inherited from DataSourceProvider.)
Protected propertyIsRefreshDeferredGets a value that indicates whether there is an outstanding DeferRefresh in use. (Inherited from DataSourceProvider.)
Public propertyMethodNameGets or sets the name of the method to call.
Public propertyMethodParametersGets the list of parameters to pass to the method.
Public propertyObjectInstanceGets or sets the object used as the binding source.
Public propertyObjectTypeGets or sets the type of object to create an instance of.
Top

  NameDescription
Protected methodBeginInitIndicates that initialization of this object is about to begin; no implicit Refresh occurs until the matched EndInit method is called. (Inherited from DataSourceProvider.)
Protected methodBeginQueryStarts to create the requested object, either immediately or on a background thread, based on the value of the IsAsynchronous property. (Overrides DataSourceProvider::BeginQuery().)
Public methodDeferRefreshEnters a defer cycle that you can use to change properties of the provider and delay automatic refresh. (Inherited from DataSourceProvider.)
Protected methodEndInitIndicates that the initialization of this object has completed; this causes a Refresh if no other DeferRefresh is outstanding. (Inherited from DataSourceProvider.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitialLoadStarts the initial query to the underlying data model. The result is returned on the Data property. (Inherited from DataSourceProvider.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnPropertyChangedRaises the PropertyChanged event with the provided arguments. (Inherited from DataSourceProvider.)
Protected methodOnQueryFinished(Object)Derived classes call this method to indicate that a query has finished. (Inherited from DataSourceProvider.)
Protected methodOnQueryFinished(Object, Exception, DispatcherOperationCallback, Object)Derived classes call this method to indicate that a query has finished. (Inherited from DataSourceProvider.)
Public methodRefreshInitiates a refresh operation to the underlying data model. The result is returned on the Data property. (Inherited from DataSourceProvider.)
Public methodShouldSerializeConstructorParametersIndicates whether the ConstructorParameters property should be persisted.
Public methodShouldSerializeMethodParametersIndicates whether the MethodParameters property should be persisted.
Public methodShouldSerializeObjectInstanceIndicates whether the ObjectInstance property should be persisted.
Public methodShouldSerializeObjectTypeIndicates whether the ObjectType property should be persisted.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventDataChangedOccurs when the Data property has a new value. (Inherited from DataSourceProvider.)
Protected eventPropertyChangedOccurs when a property value changes. (Inherited from DataSourceProvider.)
Top

  NameDescription
Explicit interface implemetationPrivate eventINotifyPropertyChanged::PropertyChangedOccurs when a property value changes. (Inherited from DataSourceProvider.)
Explicit interface implemetationPrivate methodISupportInitialize::BeginInitThis member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. (Inherited from DataSourceProvider.)
Explicit interface implemetationPrivate methodISupportInitialize::EndInitThis member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. (Inherited from DataSourceProvider.)
Top

Security noteSecurity 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.

No code example is currently available or this language may not be supported.

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 MethodThe 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.

Community Additions

ADD
Show:
© 2014 Microsoft