I am having trouble getting an image to disply in a DataGrid (WPF)… Any ideas?
Soooooo easy in WinForms DataGridView – sigh
Datatable
Dim DGV As CustomControl.DGVx = ImagesItemsGrid.FindName("ImagesItems_DGV")
Dim DS As DataSet = e.Result
Dim DT As DataTable = DS.Tables(0).Copy
Dim vDT As New DataTable
With vDT.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
.Add("Description", GetType(String))
.Add("Image", GetType(Image))
End With
Image
Dim vImageHeight As Integer = 0
For Each Row As DataRow In DT.Rows
With vDT.Rows
Dim ImageData As Byte() = DirectCast(Row("Image_Icon"), Byte())
Dim vImage As New Image
Dim vBitMap As New BitmapImage
Using vStream As New IO.MemoryStream(ImageData)
vImage.Source = BitmapFrame.Create(vStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad)
End Using
vImage.Height = 20
.Add(Row("Image_ID"), Row("Image_Name"), Row("Image_Description"), vImage)
End With
Next
DT.Dispose()
Columns
For Each Col As DataColumn In vDT.Columns
Dim vDataType As String = Col.DataType.ToString
Select Case vDataType
Case "System.Int32"
DGV.Columns.Add(New DataGridTextColumn With {.Header = Col.ColumnName, .Binding = New Binding(String.Format("[{0}]", Col.ColumnName))})
Case "System.Windows.Controls.Image"
Dim ImageCol As New DataGridTemplateColumn
ImageCol.Header = Col.ColumnName
Dim vFactory As New FrameworkElementFactory(GetType(Image))
Dim vBinding As New Binding(String.Format("[{0}]", Col.ColumnName))
vBinding.Mode = BindingMode.OneWay
vFactory.SetValue(Image.SourceProperty, vBinding)
Dim vCellTemplate As New DataTemplate
vCellTemplate.VisualTree = vFactory
ImageCol.CellTemplate = vCellTemplate
DGV.Columns.Add(ImageCol)
Case Else
Dim DGTC As New DataGridTextColumn
With DGTC
.Header = Col.ColumnName
.Binding = New Binding(String.Format("[{0}]", Col.ColumnName))
End With
DGV.Columns.Add(DGTC)
End Select
Next
Data
DS.Dispose()
DGV.DataContext = vDT
DGV.ItemsSource = vDT.DefaultView
The the text data is showing as it should but, although the column is there, nothing for the images…
Turns out the answer is just to leave the ‘image’ as Byte()
It now works