0

I set the data context of my View to the ViewModel like this

PersonVM pvm = null;

public MainPage()
    {
        this.InitializeComponent();

        pvm = new PersonVM();
        this.DataContext = pvm;
    }

then on a Button click I want to add more items to my collection

private void Btn_PointerPressed(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
    {
        pvm.DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "asdasd", Age = 23 }));
    }

Here is my ViewModel where I obviously do something wrong, but cannot figure it out..

namespace App3vv.ViewModel
{
public class PersonVMWrapper : INotifyPropertyChanged
{
    PersonModel _pm = null;

    public PersonVMWrapper(PersonModel pm)
    {
        _pm = pm;
    }

    public string Name
    {
        get
        {
            return "mr." + _pm.Name;
        }

        set { RaisePropertyChanged("Name"); }
    }

    public string Age
    {
        get
        {
            return _pm.Age.ToString() + " years";
        }

        set { RaisePropertyChanged("Age"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class PersonVM : INotifyPropertyChanged
{
    private ObservableCollection<PersonVMWrapper> personDataSource;

    public PersonVM()
    {
        this.DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "John", Age = 32 }));
        this.DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "Kate", Age = 27 }));
        this.DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "Sam", Age = 30 }));

        DataSource.CollectionChanged += DataSource_CollectionChanged;
    }
    void DataSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        this.RaisePropertyChanged("DataSource");
    }

    public ObservableCollection<PersonVMWrapper> DataSource
    {
        get
        {
            if (this.personDataSource == null)
            {
                this.personDataSource = new ObservableCollection<PersonVMWrapper>();
            }
            return this.personDataSource;
        }
        set
        {
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
}

EDIT: I implemented CollectionChange and still but my View still does not get the new item added..

v.g.
  • 1,076
  • 5
  • 19
  • 38
  • Adding an item to a collection on a property does not call the setter on that property so RaisePropertyChanged is not getting called. You need to wire up the `CollectionChanged` event – Ben Robinson Nov 05 '14 at 13:07
  • How does your binding to collection look? – galenus Nov 05 '14 at 13:08
  • How to wire up this CollectionChanged event? Is it necessary for all the collections in my ViewModel (if i have more than 1)? – v.g. Nov 05 '14 at 13:14
  • I found this http://stackoverflow.com/questions/4588359/implementing-collectionchanged but why using different List how will this reflect my view? – v.g. Nov 05 '14 at 14:02
  • `DataSource`? Why you are not adding items directly to `personDataSource`? `ObservableCollection` is good enough to be dealt with directly (to add items and **to be bound to**). – Sinatr Nov 05 '14 at 15:11
  • Where is the difference, either way it doesnt work and I cannot bind to it directly. Needs to be a property – v.g. Nov 05 '14 at 15:28

0 Answers0