我有一个动态构建的网格控件。
我正在尝试向它添加一些其他控件,我的 TextBlock 显示正常,但 Image 没有。
是的,图像源是有效的 BitmapImage。CompanyDetails 已正确填充。
这是代码:
Private Sub CreateItemContent(ByVal _Item As CompanyTyping.List, ByVal _TV As RadTileView)
Dim _Det = Company.CompanyDetails(_Item.CompanyID)
Dim item As New RadTileViewItem()
'create and define the inner grid
Dim _PG As New Grid()
Dim gridCol1 As New ColumnDefinition()
gridCol1.Width = New GridLength(200)
Dim gridCol2 As New ColumnDefinition()
gridCol2.Width = New GridLength(0, GridUnitType.Star)
_PG.ColumnDefinitions.Add(gridCol1)
_PG.ColumnDefinitions.Add(gridCol2)
'add the initial items to the initial grid
Dim _Img As New Image()
_Img.VerticalAlignment = Windows.VerticalAlignment.Top
_Img.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
_Img.Visibility = Windows.Visibility.Visible
_Img.MinHeight = 50
_Img.MinWidth = 200
_Img.Source = ByteArrayToImage(_Det(0).Logo)
Grid.SetColumn(_Img, 1)
_PG.Children.Add(_Img)
Dim _PGI As New Grid()
Dim gridRow1 As New RowDefinition()
gridRow1.Height = New GridLength(29)
Dim gridRow2 As New RowDefinition()
gridRow2.Height = New GridLength(29)
Dim gridRow3 As New RowDefinition()
gridRow3.Height = New GridLength(29)
Dim gridRow4 As New RowDefinition()
gridRow4.Height = New GridLength(0, GridUnitType.Star)
_PGI.RowDefinitions.Add(gridRow1)
_PGI.RowDefinitions.Add(gridRow2)
_PGI.RowDefinitions.Add(gridRow3)
_PGI.RowDefinitions.Add(gridRow4)
Grid.SetColumn(_PGI, 0)
_PG.Children.Add(_PGI)
Dim _TB1 As New TextBlock()
_TB1.Text = _Item.MainContact
Grid.SetRow(_TB1, 0)
_PGI.Children.Add(_TB1)
item.Header = _Item.Name & " - ID: " & _Item.CompanyID
_TV.Items.Add(item)
item.SetValue(RadTileViewItem.ContentProperty, _PG)
End Sub
和 ByteArrayToImage:
Public Function ByteArrayToImage(_Bytes As Byte()) As System.Windows.Media.Imaging.BitmapImage
If _Bytes Is Nothing Then
Return Nothing
Else
Dim _bm As New BitmapImage()
Using _strm As New MemoryStream(_Bytes)
With _bm
.BeginInit()
.CacheOption = BitmapCacheOption.OnLoad
.StreamSource = _strm
.EndInit()
End With
End Using
Return _bm
End If
End Function
我已经验证,在完整的 XAML 版本中,图像控件的源正确地显示了图像......只是不在此处