I have list which loading some long loading date. I can not use ObservableCollection because it can not be updated from another thread. What i need to do, because when I am using list nothing happens on UI * *
public class ListViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the ListViewModel class.
/// </summary>
///
private List<StructOfList> items;
public List<StructOfList> Items
{
get { return items; }
set
{
items = value;
RaisePropertyChanged("Items");
}
}
public ListViewModel()
{
Items = new List<StructOfList>();
Items.Add(new StructOfList { Amount = 10, FirstName = "Test", SecondName = "Test" });
AsyncDataLoad();
}
public void AsyncDataLoad()
{
Action<List<StructOfList>> LoadData = new Action<List<StructOfList>>(AddItemToList);
IAsyncResult result = LoadData.BeginInvoke(Items, null, null);
}
public void AddItemToList(List<StructOfList> items)
{
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(300);
items.Add(new StructOfList {FirstName = "First" , SecondName = "Second" , Amount = i});
RaisePropertyChanged("Items");
}
}
}
Xaml code goes gere
<Grid>
<ListView ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="{Binding FirstName}" Grid.Column="0"></Label>
<Label Content="{Binding SecondName}" Grid.Column="1"></Label>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>