我有2个班,Fleet和Car,一个Fleet可以有多个Cars,两个类都是ObservableCollections。
当我点击按钮时,我创建了一个按钮,它必须删除ListBox中选定的Fleet:
private void btRemoveFleet_Click(object sender, RoutedEventArgs e)
{
Fleets.Remove((Fleet)lbFleets.SelectedItem);
}
当我单击以下代码中的按钮时,会发生错误:
private void lbFleets_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
Cars = ((Fleet)lbFleets.SelectedItem).Car;
lbCars.ItemsSource = Cars;
grdCarOverview.DataContext = Cars;
}
private void lbFleets_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
Cars = ((Fleet)lbFleets.SelectedItem).Car;
lbCars.ItemsSource = Cars;
grdCarOverview.DataContext = Cars;
}
我得到的错误
System.NullReferenceException:'对象引用未设置为对象的实例。'
我对C#很陌生,只是想知道发生了什么以及如何修复它。