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;
}
}
}