How to: Make Data Available for Binding in XAML
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.
Example
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 Name="root"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:SDKSample"
xmlns:system="clr-namespace:System;assembly=mscorlib"
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.