WPF:ListView中的图像未显示

时间:2013-04-15 09:13:09

标签: wpf image listview binding

我有一个ListView,每行应包含一个图像和文本。文本的绑定工作正常,但图像不显示。

我的XAML-Markup:

<ListView Name="lvUpgrade">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="20px">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="{Binding Path=Icon}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="75px" DisplayMemberBinding="{Binding Path=Time, StringFormat={}{0:HH:mm:ss}}" />
            <GridViewColumn Width="300px" Header="Nachricht" DisplayMemberBinding="{Binding Path=Message}" />
        </GridView>
    </ListView.View>
</ListView>

我的代码背后:

Public Class Upgrade

    Public Sub AddMessage(ByVal message As Message)
        Me.lvUpgrade.Items.Add(message)
    End Sub

    Public Class Message

        Public Enum MessageType

            Normal
            Information
            Warning
            [Error]
        End Enum

        Public Sub New(ByVal type As MessageType, ByVal message As String)
            _Type = type
            _Message = message
        End Sub

        Private _Type As MessageType = MessageType.Normal
        Public ReadOnly Property Type As MessageType
            Get
                Return _Type
            End Get
        End Property

        Private _Message As String = String.Empty
        Public ReadOnly Property Message As String
            Get
                Return _Message
            End Get
        End Property

        Private _Time As DateTime = Now
        Public ReadOnly Property Time As DateTime
            Get
                Return _Time
            End Get
        End Property

        Public ReadOnly Property Icon As System.Drawing.Image
            Get
                Select Case Me.Type
                    Case MessageType.Information
                        Return My.Resources.Information16
                    Case MessageType.Warning
                        Return My.Resources.Alert16
                    Case MessageType.Error
                        Return My.Resources.Error16
                    Case Else
                End Select

                Return Nothing
            End Get
        End Property
    End Class
End Class

要绑定图像,我使用一个属性,该属性从本地资源返回图像。我使用相同的方式将图像绑定到控件,就像我为文本做的那样。这是错误的还是为什么不起作用?

感谢您的回复。

1 个答案:

答案 0 :(得分:3)

您必须将Image转换为BitmapImage

为此,您可以在单独的模块中定义以下扩展。 这使您有机会在代码中使用的每个图像上使用该功能:

Module Extensions

  <Extension()>
  Public Function ToBitmapImage(ByVal lBitmap As Bitmap) As BitmapImage
    Dim lMemoryStream As New MemoryStream()
    Dim lBitmapImage As New BitmapImage()

    lBitmap.Save(lMemoryStream, ImageFormat.Png)
    lBitmapImage.BeginInit()
    lBitmapImage.StreamSource = New MemoryStream(lMemoryStream.ToArray())
    lBitmapImage.EndInit()
    Return lBitmapImage
  End Function

End Module

之后你必须编辑你的财产,如:

Public ReadOnly Property Icon As BitmapImage
  Get
    Select Case Me.Type
      Case MessageType.Information
        Return My.Resources.Information16.ToBitmapImage
      Case MessageType.Warning
        Return My.Resources.Alert16.ToBitmapImage
      Case MessageType.Error
        Return My.Resources.Error16.ToBitmapImage
      Case Else
    End Select

    Return Nothing
  End Get
End Property