We have an ItemsControl setup like this
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Items}" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Data}"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
with the MainViewModel being:
public class MainViewModel : ViewModelBase
{
public ObservableCollection<AutoUpdatingItem> Items
{
get;
set;
}
public MainViewModel()
{
Items = new ObservableCollection<AutoUpdatingItem>();
for (int i = 0; i < 1000; i++)
{
Items.Add(new AutoUpdatingItem(i));
}
}
}
and the ViewModel of the row
public class AutoUpdatingItem : ViewModelBase
{
private readonly DispatcherTimer timer;
private int data;
public int Data
{
get { return data; }
set { Set(ref data, value); }
}
public AutoUpdatingItem(int i)
{
Data = i;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(20);
timer.Start();
timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
// Data would be fetched from WebService here
Data++;
}
}
How can we achieve that only the visible items are updated? Is there any way to get notified when an item in the virtualized panel is being recycled?