Thursday, May 9, 2013

Specifying the Selected Items in a ListBox with Multiple Selection Enabled

The project obviously isn't finished yet, and some parts of it link to other projects in the solution, but here are the primary relative pieces:

<TextBox x:Name="txtEditAddStore"/>
<Button x:Name="btnEditAddStore" Content="Add Store"/>
<ListBox x:Name="lstEditStores" SelectionMode="Multiple" DisplayMemberPath="Name" ItemsSource="{Binding Stores}">


Public Class Store
Public Property Id() As Byte
Public Property Name() As String
Public Property Address() As String
Public Property Website() As String
Public Sub New()
End Sub
Public Sub New(i As Byte, n As String, addr As String, web As String)
Me.Id = i
Me.Name = n
Me.Address = addr
Me.Website = web
End Sub
End Class

Public Class ObservableStores
Public Property Stores() As ObservableCollection(Of Store)

Public Sub New()
Me.Stores = New ObservableCollection(Of Store)(CType(Application.Current, App).Stores.OrderBy(Function(s) s.Name))
AddHandler Me.Stores.CollectionChanged, AddressOf Me.Stores_CollectionChanged
End Sub

Private Sub Stores_CollectionChanged(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
End Sub
End Class

And I use the following line to initialize the ListBox:

Me.lstEditStores.DataContext = New ObservableStores()

And I use the following code to add items (which is when the error occurs):

Private Sub btnEditAddStore_Click(sender As Object, e As RoutedEventArgs) Handles btnEditAddStore.Click
CType(Me.lstEditStores.DataContext, ObservableStores).Stores.Add(CType(Application.Current, App).GetStoreById(id))
End Sub

If you need more detail about any of the code (or I forgot something), let me know, I will be happy to give it to you, or if you can tell me where to do it, I am even willing to upload the entire project and related projects (but that seemed rather unnecessary for just this problem). Thanks.





Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/


No comments:

Post a Comment