I have a ListView
that is used severally and each case needs to show items of different derived classes of the same abstract class. I want to search the list and remove items from the list. Needless, to say that it would be convenient if I could bind to the base class and encapsulate that in a UserControl
.
Concretely, the abstract class
Public MustInherit Class SearchableItem
Public ReadOnly Property Text As String
End Class
and the derived one
Public Class Series
Inherits SearchableItem
Public Property Title As String
Public Property Desc As String
Public Overloads ReadOnly Property Text As String
Get
Return Title
End Get
End Property
End Class
and this shows up in
Public Class SomeAvatar
Public Property SeriesList As New ObservableCollection(Of Series)
End Class
finally, the UserControl
has a dependency property ItemList
that the ListView
displays.
Public Property ItemList As ObservableCollection(Of SearchableItem)
Get
Return GetValue(ItemListProperty)
End Get
Set(ByVal value As ObservableCollection(Of SearchableItem))
SetValue(ItemListProperty, value)
End Set
End Property
Public Shared ReadOnly ItemListProperty As DependencyProperty =
DependencyProperty.Register("ItemList",
GetType(ObservableCollection(Of SearchableItem)), GetType(ucListView),
New PropertyMetadata(Nothing))
The problem is that ItemList
is empty. I have checked all the Loaded events of parents of the UserControl
and they all have the collection complete. I then passed the collection as part of the DataContext
of the UserControl
and got the error that said Unable to cast observablecollection(of base) to observablecollection(of derived)
(paraphrased).
Isn't a collection of dogs also a collection of animals? I have to be missing some basic thing or is this something to do with WPF?