WPF DataGrid Does Not Show All Lines

advertisements

I have an ObservableCollection bound to a WPF DataGrid. The ObservableCollection is populated and updated asynchronously.

The DataGrid is not displaying the correct number of rows. Each time I run the application I get a varying number rows displayed, usually 7-8 but sometimes only one. I get the same behvaior with ListBox, but not with ComboBox.

I get this same behavior with a ListBox and ItemsControl but not with ComboBox. The ComboBox correctly displays all 18 items as expected.

EDIT

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.

I'll need to do some more detective work here as my presumption was the updates were happening in the same thread. So the reason for the first few items being displayed correctly is that the ViewModel was able to retrieve some of the data before the View itself loaded.


Use this code:
(Notice that DownloadedItem and it´s properties must also be using INotifyPropertyChanged as in this example)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    class Downloads : NotifyBase
    {
        public virtual ObservableCollection<DownloadedItem> DownloadedItemCollection
        {
            get { return _DownloadedItemCollection; }
            set { _DownloadedItemCollection = value; OnPropertyChanged(System.Reflection.MethodBase.GetCurrentMethod().Name.Substring(4));  /*OnPropertyChanged("DownloadedItemCollection");*/ }
        } private ObservableCollection<DownloadedItem> _DownloadedItemCollection;

    }
}

public class NotifyBase : INotifyPropertyChanged
{
    #region NotifyBase
    // Declare the event
    public virtual event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}