0

I am having some strange problems with my ObservableCollection. I am using .Net 4.0.

I have a viewmodel like this:

PoViewModel : DataViewModelBase
{
    public ObservableCollection<PoDetail> PoDetails { <omitting dp boiler code for ease> }

    public void Add()
    {
        this.PoDetails.Add(new PoDetail()); //<-- this is not getting detected in the wpf UI
    }
}

DataViewModelBase is a DependencyObject

I have this bit of test code that is verifying nothing is happening:

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
    base.OnPropertyChanged(e);
    this.IsDirty = true;
    MessageBox.Show(e.Property.Name + " just changed");
}

What could be the problem? I am assuming I don't understand how ObservableCollections work but from everything I have read this should be working fine.

Price Jones
  • 1,948
  • 1
  • 24
  • 40

1 Answers1

1

Why do you need a dependency property on your viewModel?

Simply implementing INotifyPropertyChanged should be enough.

Also, you don't expect the property of name "PoDetails" to be updated when adding an element to the ObservableCollection. You should expect the CollectionChanged event of the ObservableCollection instance to be raised.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • My view model has a dependency property on it because that is how I was taught to hook up view models to wpf ui's. – Price Jones Sep 20 '13 at 16:18
  • 1
    @PriceJones That generally not required. INotifyPropertyChanged is "better" IMO. See a great MVVM example here: http://rachel53461.wordpress.com/2011/05/08/simplemvvmexample/ See a discussion of INPC vs dependency object here: http://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel – ken2k Sep 20 '13 at 16:22