My application uses a WPF DataGrid
. One of the columns is a template column that contains a ComboBox
bound to an ObservableCollection
Finally I found the reason for the exception. The problem occurs when you are removing the selected item from the list. Below I've posted an incomplete piece of code.
<!-- XAML -->
<ListBox ItemsSource="{Binding Rooms}" SelectedItem="{Binding SelectedRoom}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button Content="remove" DockPanel.Dock="Right" Command="{Binding Some Remove Command}" />
<TextBlock Text="{Binding Name}" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And the view model:
// View Model
public ObservableCollection<Room> Rooms { get; private set; }
public Room SelectedRoom { get; set; }
public RemoveRoom()
{
var room = SelectedRoom;
//SelectedRoom = null; // Uncomment this line to avoid the exception
Rooms.Remove(room);
}
The solution is to first set the selected item to null (or any other item) before actually removing the item from the list.
Oliver Hanappi
Is your Room class object actually instantiated? It looks as if your adding an uninitialised value.