将图像添加到WPF DataGrid

时间:2013-01-07 22:06:51

标签: wpf image datagrid

我无法在DataGrid(WPF)中显示图像......有什么想法吗?

Soooooo在WinForms DataGridView中轻松 - 叹息

数据表

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

图像

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()

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

数据

DS.Dispose()
            DGV.DataContext = vDT
            DGV.ItemsSource = vDT.DefaultView

文本数据显示应该显示,但是,虽然列在那里,但图像没有...

1 个答案:

答案 0 :(得分:0)

原来答案就是将'image'留作Byte()

现在有效

Case "System.Byte[]"

                        Dim ImageTemp As New DataTemplate
                        Dim ImageCol As New DataGridTemplateColumn
                        Dim vFactory As New FrameworkElementFactory(GetType(Image))
                        Dim vBinding As New Binding(Col.ColumnName)
                        vFactory.SetBinding(Image.SourceProperty, vBinding)
                        ImageTemp.VisualTree = vFactory
                        ImageCol.CellTemplate = ImageTemp
                        DGV.Columns.Add(ImageCol)