0

Editing this entire post to clarify... I cannot seem to nail this:

BackgroundWorker receives data from a WCF service that is a list of objects. The service reference is configured to be ObservableCollection.

I pass the ObservableCollection via a delegate into my main UI thread and set it equal to the UI threads Local Collection.

A listbox is bound to this local collection and does not update. I've added the following to my collection:

public ObservableCollection<EmployeeData> _empData { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

public ObservableCollection<EmployeeData> EmpData
    {
        get { return _empData ; }
        set
        {
            _empData = value;
            OnPropertyChanged("EmpData");
        }
    }

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

This even fires but the PropertyChanged is always null. My XAML listbox has a binding declared as:

ItemsSource="{Binding Path=EmpData}"

No matter what I do EmpData updates but the ListBox does not, I've tried several other methods but nothing ever changes in the listbox, its always just null.

I've been working on this for over a day now, I cannot seem to get this whole automatic updating thing to 'click'.

1 Answers1

0

I'm not sure that I understand exactly what you are doing, but here are a couple of suggestions.

  • Have a single ObservableCollection
  • Bind your itemcollection (or listbox, or whatever) to this
  • Depending on the user, clear and fill that observablecollection with list data
  • Have the background worker update the list and refresh the observable collection if anything has changed.

Ideally your EmployeeData class will implement the INotifyPropertyChanged interface, so that property changes will get automatically updated in your view.

mdm20
  • 4,475
  • 2
  • 22
  • 24
  • I made some changes and added a simple ICollectionView and set it to GetDefaultView on the employeelist. I added items during runtime as a test locally and they do show in the list, however, the data coming in does not fire the INotifyPropertyChange. It is set that way remotely(the class implements that interface) and the service configuration does allow databindings, so I'm not sure why it would not update on its own. – RageMachine Aug 14 '13 at 22:07
  • The code looks fine. Probably a binding issue. Try setting the datacontext of your class to itself in the contructor – mdm20 Aug 15 '13 at 19:21
  • I gave it a shot to no avail, it just doesn't do it unless i first run clear() then repopulate. So i ended up setting itemssource again on the object since i ran out of ideas and patience. i have a feeling its because i specify the entire collection to be replaced as _empData = _newData; and its not acting accordingly, but replacing each object would be to slow. – RageMachine Aug 15 '13 at 21:19