0

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

在 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

文本数据按应有的方式显示,但是,尽管该列在那里,但图像没有任何内容...

4

1 回答 1

0

原来答案只是将“图像”保留为 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)
于 2013-01-09T17:58:31.403 回答