Como: 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.
Exemplo
Se você tiver um common language runtime (CLR) você gostaria de vincular a partir do objeto XAML, uma forma, você pode disponibilizar o objeto de vinculação é defini-la como um recurso e dê a ele um x:Key. In the following example, you have a Person object with a string property named PersonName. O Person objeto é definido no namespace chamado SDKSample.
<Window
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://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}"/>
Nesse exemplo específico, o resultado é o mesmo: Você tem um TextBlock com o conteúdo de texto 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.
Private myDataSet As DataSet
Private Sub OnInit(ByVal sender As Object, ByVal e As EventArgs)
Dim mdbFile As String = Path.Combine(AppDataPath, "BookData.mdb")
Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile)
Dim conn As New OleDbConnection(connString)
Dim adapter As 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
End Sub
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;
}
Acesso XML dados para vinculação usando a XmlDataProvider da classe, consulte Como: Bind to XML Data Using an XMLDataProvider and XPath Queries. Acesso XML dados para vinculação usando a ObjectDataProvider da classe, consulte Como: Vincular XDocument, XElement ou LINQ para XML resultados de consulta.
For information about the different ways you can specify the data you are binding to, see Como: Especificar a Fonte de Associação. Para obter informações sobre como implementar seus próprios ou de que tipos de dados que você pode vincular a common language runtime (CLR) objetos para vinculação, consulte Visão geral sobre associação de fontes.
Consulte também
Conceitos
Revisão de Associação de Dados