wpfobservablecollectiondb4o

db4o activatablelist vs wpf observablecollection


I'm currently creating a program with a db4o back-end. The front-end is WPF. Now I'm having a small problem because the db4o activatablelist is blocking the observablecollection from WPF.

I can't seem to find a why to keep them synced in twoway mode... If I add one through GUI (WPF) then it should be added to the activatablelist.


Solution

  • Well I'm not an expert on WPF and the databinding stuff. But I would use the CollectionChanged-event of the observable collection to update the activatable collection. Then you pack this functionality in a nice utility class/method to be used where needed.

    The idea is to do something like this:

    IList<T> regularList = // method-parameter, or from somewhere else;
    ObservableCollection<T> observableCollection
        = new ObservableCollection<T>(regularList);
    
    observableCollection.CollectionChanged +=
        (sender, eventArgs) =>
        {
            if (null != eventArgs.NewItems)
            {
                for (int i = 0; i < eventArgs.NewItems.Count; i++)
                {
                    regularList.Insert(i + eventArgs.NewStartingIndex, (T)eventArgs.NewItems[i]);
                }
            }
            if (null != eventArgs.OldItems)
            {
                for (int i = 0; i < eventArgs.OldItems.Count; i++)
                {
                    regularList.RemoveAt(i + eventArgs.OldStartingIndex);
                }
            }
        };
    

    I guess such thing exist in other WPF projects too, for regular lists / ORM-lists.