views:

172

answers:

1

Hi.

Last time I started to implement a Model View ViewModel to work easily with WPF. I have to say, that it's elegant, but there's one thing I have to remark. I noticed a performance issue when working with MVVM.

Wrapping the original object into an "Viewable" object can be expensive in case of many items. I recently had a ListView with about 20 000 items. It took several seconds to create the View. Am I doint anything wrong or it's quite logical. If so, how to solve this problem ?

My example code:

public class AdresseVm : INotifyPropertyChanged
    {        
        public AdresseVm(Adresse adresse)
        {
            this.adresse = adresse;
        }

        private bool isChecked;
        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                OnPropertyChanged("IsChecked");
            }
        }

        private Adresse adresse;
        public Adresse Adresse
        {
            get { return adresse; }
            set
            {
                adresse = value;
                OnPropertyChanged("Adresse");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }

and a main object:

public class AdressenVm
    {
        private IEnumerable<Adresse> adressen;
        private ObservableCollection<AdresseVm> adressenListVm = new ObservableCollection<AdresseVm>();

        public AdressenVm(IEnumerable<Adresse> adressen)
        {
            this.adressen = adressen;

            foreach (Adresse adresse in adressen)
                adressenListVm.Add(new AdresseVm(adresse));
        }

        public ObservableCollection<AdresseVm> AdressenListVm
        {
            get { return adressenListVm; }
            set { adressenListVm = value; }
        }

        public IEnumerable<Adresse> CheckedAdresses
        {
            get
            {
                foreach (AdresseVm a in adressenListVm)                
                    if (a.IsChecked) yield return a.Adresse;                
            }
        }
    }
+2  A: 

Dan Crevier in part 6 of his series about MVVM (one of the first appearances of MVVM, he calls it DM-V-VM...) touch this topic:

If you have a large set of items, you only want to keep the visible items live. We'll do this by giving models Activate and Deactivate functions that control when it is live. (...)

Let's say we've got thousands of our models in a ListBox. It's only going to only show a few of them on the screen at a time and we only want the ones on screen to be active. We'll use the attached property trick to do this without having to write custom code each time we want to activate and deactivate models...

More recently Bea Stollnitz also give a solutions to this problem here. I hope this can useful to you.

DaniCE