Cómo: Enlazar a un origen de datos ADO.NET
En este ejemplo se muestra cómo enlazar un control ListBox de Windows Presentation Foundation (WPF) a un DataSet de ADO.NET.
Ejemplo
En este ejemplo, se utiliza un objeto OleDbConnection para conectar al origen de datos, que es un archivo Access MDB especificado en la cadena de conexión. Una vez establecida la conexión, se crea un objeto OleDbDataAdpater. El objeto OleDbDataAdpater ejecuta una instrucción select de Structured Query Language (SQL) para recuperar el conjunto de registros de la base de datos. Los resultados del comando de SQL se almacenan en una DataTable de DataSet llamando al método Fill de OleDbDataAdapter. El elemento DataTable de este ejemplo se denomina BookTable. A continuación, en el ejemplo se establece la propiedad DataContext de ListBox en el objeto DataSet.
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;
}
Después, se puede enlazar la propiedad ItemsSource de ListBox al elemento BookTable de DataSet:
<ListBox Name="myListBox" Height="200"
ItemsSource="{Binding Path=BookTable}"
ItemTemplate ="{StaticResource BookItemTemplate}"/>
BookItemTemplate es el objeto DataTemplate que define cómo aparecen los datos:
<StackPanel.Resources>
<c:IntColorConverter x:Key="MyConverter"/>
<DataTemplate x:Key="BookItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=Title}" Grid.Column="0"
FontWeight="Bold" />
<TextBlock Text="{Binding Path=ISBN}" Grid.Column="1" />
<TextBlock Grid.Column="2" Text="{Binding Path=NumPages}"
Background="{Binding Path=NumPages,
Converter={StaticResource MyConverter}}"/>
</Grid>
</DataTemplate>
</StackPanel.Resources>
IntColorConverter convierte un valor int en un color. Al utilizar este convertidor, el color de la propiedad Background del tercer objeto TextBlock aparece verde si el valor de NumPages es inferior a 350 y rojo si no es así. La implementación del convertidor no se muestra aquí.
Vea también
Referencia
Conceptos
Información general sobre el enlace de datos