You are getting the exception here because you are making the itemsource to null and then trying to clear the items in it(rather it should be the opposite)
1. Use
Observable Collection Which is meant for performing (Read,Update,Delete) operation dynamically to a collection
2.No need to make the itemsource to null, rather just clear the itemsource and re-assign with new values.
Improved Answer:
Try below code for using ObservableCollection in ListView
private ObservableCollection<TimeStepData> timeStepDataCollection;
public ObservableCollection<TimeStepData> TimeStepDataCollection
{
get
{
if (timeStepDataCollection== null)
{
timeStepDataCollection= new ObservableCollection<TimeStepData>();
}
return timeStepDataCollection;
}
set
{
timeStepDataCollection = value;
}
}
//created a class for TimeStepData
public class TimeStepData
{
public string TimeStepItem1{ get; set; }
public string TimeStepItem2{ get; set; }
public string TimeStepItem3{ get; set; }
}
// In XAML :
<ListView Height="96" Name="listView1" Width="226" ItemsSource="{Binding TimeStepDataCollection}">
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="Name" DisplayMemberBinding="{Binding TimeStepItem1/>
<GridViewColumn Width="70" Header="Class" DisplayMemberBinding="{Binding TimeStepItem2/>
<GridViewColumn Width="70" Header="Status" DisplayMemberBinding="{Binding TimeStepItem3/>
</GridView>
</ListView.View>
</ListView>