WPF ObservableCollection问题 [英] WPF ObservableCollection problem
问题描述
新手编码员需要帮助,
我设置了一个显示图像和文本的组合框(效果很好),但是为了我的生命,我无法更改类以在图像路径中传递,下面是类代码,如果有人可以调整代码,我将永远地很棒而不是使用硬编码的路径.
提前谢谢.
''------------------------------------------------ -------------
Hi all, newbie coder needs help please,
I have a combobox set up displaying images and text (works great) but for the life of me I can''t change the class to pass in image paths, below is the class code, I would be eternally greatful if someone can adjust code to be useful rather than use hard coded paths.
Thanks in advance.
''-------------------------------------------------------------
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Text
Public Class ImageFactory
Public Shared count As Integer = 0
Private Shared _myactualdata As New Images()
Public Shared ReadOnly Property MyImages() As Images
Get
Return _myactualdata
End Get
End Property
Public Class Images
Inherits ObservableCollection(Of ImageData)
Public Sub New()
' this works but not much use to me as it is.
Me.Add(New ImageData("Resources\Images\0.jpg"))
Me.Add(New ImageData("Resources\Images\1.jpg"))
Me.Add(New ImageData("Resources\Images\2.jpg"))
Me.Add(New ImageData("Resources\Images\3.jpg"))
Me.Add(New ImageData("Resources\Images\4.jpg"))
Me.Add(New ImageData("Resources\Images\5.jpg"))
Me.Add(New ImageData("Resources\Images\6.jpg"))
Me.Add(New ImageData("Resources\Images\7.jpg"))
Me.Add(New ImageData("c:\test\100_DSC_1727-s.jpg"))
End Sub
End Class
Public Class ImageData
Implements INotifyPropertyChanged
'
Private _sImageName As [String] = ""
Public Sub New(ByVal sImageName As String)
ImageFactory.count += 1
ImageName = sImageName
End Sub
Public Property ImageName() As [String]
Get
Return _sImageName
End Get
Set(ByVal value As [String])
_sImageName = value
NotifyPropertyChanged(ImageName)
End Set
End Property
Protected Sub NotifyPropertyChanged(ByVal sProp As [String])
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(sProp))
End Sub
Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class
End Class
推荐答案
您的代码中存在许多小错误.一种是您在更改属性时创建了一个自定义通知,并且它需要一个字符串,因此可以这样做:
There are a number of minor mistakes in you code. One is that you created a custom notifycation when changing the properties and it wants a string, so do that:
NotifyPropertyChanged("ImageName")
您需要此类的唯一原因是您想知道成员是否获得了更新的源,例如Url的更改等.ObservableCollection是在集合更新时触发事件的,例如添加项目和删除项目.物品.但是,当项目被更改/更新时,它不会触发事件.
为何将此类共享为只读?
The only reason you need this class is the you want to know if a memeber gets an updated source, like a change in the Url etc. The ObservableCollection was made to fire an event when the collection gets updated, like adding an item and removing an item. It does however not fire an event when an item gets changed/updated.
And why did you make this class shared readonly?
Public Shared ReadOnly Property MyImages() As Images
通常不需要共享列表,因为新类的所有新实例都将看到相同的列表.
为了使代码根据您的规范工作,您只需要ImageData类,一个ObservableCollection即可,而无需其他代码.您可以简单地将可观察的集合绑定到任何列表,网格或其他任何东西.
A shared list is usually not needed, as the same would be seen by all new instances of the new class.
To make make the code work according to your specs, you only need the ImageData class, An ObservableCollection and not the other code. You could simply bind the observable collection to any list, grid or whatever.
这篇关于WPF ObservableCollection问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!