我将数据加载到DataGrid控件中(dataGridImagem使用此代码:
private bool carregarDados()
{
bool status = false;
try
{
ligacaoBD.Open();
MySqlCommand comandoMySQL = new MySqlCommand("Select * from imagem", ligacaoBD);
MySqlDataAdapter adp = new MySqlDataAdapter(comandoMySQL);
DataSet ds = new DataSet();
adp.Fill(ds, "carregarDados");
dataGridImagem.DataContext = ds;
status = true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ligacaoBD.Close();
}
return status;
}
XAML看起来像这样:
<DataGrid Grid.Row="0" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" Name="dataGridImagem" ItemsSource="{Binding Path=carregarDados}" Margin="5" SelectionMode="Single" CanUserAddRows="False" CanUserDeleteRows="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=idImagem}" Header="Código" Width="Auto" IsReadOnly="True" />
<DataGridTemplateColumn Header="Imagem" IsReadOnly="True" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding imagemImagem}" Width="10" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Path=imagemDescricao}" Header="Imagem" Width="*" IsReadOnly="True" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Image Height="100" Source="{Binding imagemImagem}" />
</DataTemplate>
</DataGrid.RowDetailsTemplate>
如何将与所选记录关联的图像加载到图像控件中?