Sorry if the Syntax is off, i typed this on Notepad, (temp. issues with computer with VS)
i have a class Movie with a Title property, i have
Dim movieList = New ObservableCollection(of Movie)
Private Sub SelectMovie(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
For Each m As Movie in movieList
If lb1.SelectedItem = "New" Then
m.Add(New Movie())
End If
Next
End Sub
movieList was initialized with temp values, last one being “New”.
In the constructor i iterated through movielist and put stuff in the listbox.
lb1.Items.Add(m)
In the MainWindow.xaml i have
<Grid Name="moviePage" >
<ListBox Name="lb1" SelectionChanged="SelectMovie">
</Grid>
I’m aware that this approach is wrong as I’m modifying a list while using it, but i can’t seem to find a way around this.
The idea is to simply have the listbox show a bunch of movies, with the ability to add a new movie if “New” is selected from the list.
Here’s a working code sample:
Having said that, I don’t believe this is the right approach for what you’re doing. I think you’re better off changing the template for the ListBox so that it shows the list of items it’s bound to, and then a “New” button or link at the bottom (outside of the list).
Here’s a very simple example:
So now you have a ListBox which includes a button to create a new movie. In your
AddMovieevent handler you can add a newMovieinstance to the list and select it.