我将对象直接添加到ListBox,在这个类中,我有一个BitmapImage对象。 我正在使用ItemTemplate:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Path=ElementIcon}"></Image>
<TextBlock Text="{Binding Path=ElementName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
我直接添加了这个类的对象:
Public Class ExplorerClass
Implements INotifyPropertyChanged
Public Property ElementType As String = Nothing
Public Property ElementName As String = Nothing
Public Property ElementContainer As String = Nothing
Public Property ElementIcon As New BitmapImage
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Public Sub New(ByVal WantedElementContainer As String, ByVal WantedElementName As String, ByVal WantedElementType As String)
ElementType = WantedElementType
ElementName = WantedElementName
ElementContainer = WantedElementContainer
Dim str As New MemoryStream
Dim IWorking As Icon = showIcon(ElementName.Substring(ElementName.LastIndexOf(".")))
IWorking.ToBitmap.Save(str, System.Drawing.Imaging.ImageFormat.Png)
ElementIcon.BeginInit()
ElementIcon.StreamSource = str
ElementIcon.EndInit()
NotifyPropertyChanged("ElementIcon")
End Sub
End Class
但是,没有图片显示; 所以,我的问题是:“如何绑定BitmapImage”?
答案 0 :(得分:1)
看起来您没有实施INotifyPropertyChanged
Interface。看看this page on MSDN。我不确定这是否也是一个问题,但请尝试设置图像的高度和宽度。