0

I have a WPF application with a screen containing a tab control with two tabs. On each tab is a datagrid, each one bound to an ObservableCollection of Part objects. The part has a few "quantity" properties, which need to be synchronized between the grids. For example, if the user changes the quantity of partABC on grid1, partABC either needs to be added to grid2 with the same quantity, or if grid2 already contains partABC, then its quantity must be changed to reflect grid1.

My problem is that this must work in both directions. If I set a PropertyChanged handler on every part in both grids, I end up with an infinite loop as they constantly update each other's quantities. Up until now, I was handling this during the tab control selection changed event, just iterating through one of the lists and setting quantities one-by-one. This worked well enough until I realized that the users could potentially add thousands of parts to their lists, and at that point this process takes an unacceptable amount of time to complete (around 25 seconds for 4500 part objects).

edit
The first grid contains every part in the database, serving as sort of a "pick-list" where users simply scroll to the part they are looking for and enter quantities. The second grid contains only parts which have been manually entered by the user, in the event that they prefer to type in the numbers of the parts they want. Grid2 is always a subset of grid1.

drowned
  • 530
  • 1
  • 12
  • 31
  • 1
    There won't be an infinite loop if you compare new value with existing and don't raise property changed event if they are equal. For instance, `set { if(value != _myproperty){_myproperty=value; RaisePropertyChanged(()=>MyProperty);}}` – Dmitry Sep 21 '11 at 15:22

1 Answers1

1

You can accomplish this through databinding. You should not create duplicate Part objects. Instead duplicate the collections that hold the parts.

Part sharedPart = new Part();
Part onlyInTabA = new Part();
Part onlyInTabB = new Part();

ObservableCollection<Part> tabAParts = new ObservableCollection<Part>() { sharedPart, onlyinTabA };
ObservableCollection<Part> tabBParts = new ObservableCollection<Part>() { sharedPar, onlyInTabB };

Now use tabAParts to databind to the grid on tab A and tabBParts to databind to the grid on tab B

If your Part class implements INotifyPropertyChanged then changing a property of sharedPart will update both grids on both tabs. When you add a new part you can choose to make it shared (add it to both collections) or to keep it tab-specific

cordialgerm
  • 8,403
  • 5
  • 31
  • 47
  • Sorry, I just realized my question is missing some information. Edited initial post for clairty. – drowned Sep 21 '11 at 15:17
  • Actually I suppose that this idea will still work, I just need to add the existing part from the tabA collection whenever I need to insert into the tabB collection. Excellent, thanks a lot. – drowned Sep 21 '11 at 17:05