I'm new to WPF and am having problems binding data to a simple ListBox. I've set it up in MainWindow.XAML
<ListBox Name="lbxShows" />
then in the code behind, I set the ItemsSource property to be an ObservableCollection of Show objects called ShowList. This is actually a property of another class (Oasis) of which OasisInst is an instance (setup in the constructor of MainApplication).
InitializeComponent();
mainApp = new MainApplication();
lbxShows.ItemsSource = mainApp.OasisInst.ShowList;
At this point, there are no items in ShowList but later, some get added and they don't appear in the ListBox.
The code for the Oasis class implements the INotifyPropertyChanged interface and then has the textbook method that's called from the ShowList setter.
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
PropertyChanged is my PropertyChangedEventHandler event.
When I step through in debug mode, PropertyChanged is null so it seems that nothing has subscribed to my event. Given that this would usually happen automatically through a binding (I think?), then I'm guessing the binding hasn't been setup properly.
Maybe setting the ItemsSource property alone isn't sufficient to setup the binding?
ShowListproperty, this is irrelevant. If you do change the value of theShowListproperty, i.e. create a whole new collection and set the property value to reference that collection, then it's not sufficient to just have implemented the interface. You need to actually bind the property to the targetItemsSourceproperty, instead of just setting it. Typically this would be done in XAML, though you can also do it in code-behind if you insist. - Peter Duniho