I've got a Windows Phone app with a ListPicker bound to an ObservableCollection and a selected item:
<toolkit:ListPicker Header="Endgame condition" ItemsSource="{Binding Conditions}"
SelectedItem="{Binding SelectedCondition, Mode=TwoWay}" />
And in the ViewModel:
public ObservableCollection<string> Conditions
{
get { return conditions; }
}
public string SelectedCondition
{
get { return selectedCondition; }
set
{
if (selectedCondition != value)
{
selectedCondition = value;
OnPropertyChanged("SelectedCondition");
}
}
}
When I try to remove the selected item from the list in a button handler, I get an InvalidOperationException (SelectedItem must always be set to a valid value):
public void ConfirmCondition()
{
var selected = selectedCondition;
SelectedCondition = null;
// throws an exception: SelectedItem must always be set to a valid value
conditions.Remove(selected);
}
I was hoping that setting the SelectedCondition to null (which is actually the first item in the list), would fix it, but it doesn't. It appears that the NotifyPropertyChanged on the SelectedCondition (which is bound to the ListPicker's SelectedItem with a TwoWay binding mode) doesn't update the SelectedItem before the Remove triggers a CollectionChanged notification for the ItemsSource.
Any ideas how I can remove the item without getting the exception?
Exception
that you only just mention in your very last sentence? – Sheridan