Export (0) Print
Expand All

ObjectDataProvider.ObjectType Property

Gets or sets the type of object to create an instance of.

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

public Type ObjectType { get; set; }
<object ObjectType="myDataType"/>

XAML Values

myDataType

The data type to create an instance of. Use the x:Type Markup Extension to set a type.

Property Value

Type: System.Type
This property is null when the ObjectDataProvider is uninitialized or explicitly set to null. If ObjectInstance is assigned, ObjectType returns the type of the object or null if the object is null. The default value is null.

ExceptionCondition
InvalidOperationException

ObjectDataProvider is assigned both an ObjectType and an ObjectInstance; only one is allowed.

If DeferRefresh is used, ObjectInstance and Data do not update until Refresh is called.

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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft