RayOldProf
RayOldProf

Reputation: 1070

Catel (MVVM framework) ObservableCollection

I'm Using Catel to implement a WPF Application.

I have a class that extends from ObservableCollection and every time an Item is inserted The UI must be updated.

CODE (simplified version) :

  public abstract class LogCollections : ObservableCollection<Log4NetLog>  {

    private readonly Object _locker;

    protected LogCollections() {
        _logChart = new LoggingLevelChart();
        _locker = new object();
    }

    public object Locker {
        get { return _locker; }


    protected override void InsertItem(int index, Log4NetLog item) {
        lock (_locker) {
            base.InsertItem(index, item);

            if (item == null) {
                return;
            }
            Log4NetLog temp = item as Log4NetLog;

            // Updating

            if (temp != null) {

                // Updating
            }
        } //UnLock 
    }

  }
}

Until Now I have used BindingOperations.EnableCollectionSynchronization which is only available in .NET 4.5. Unfortunately I have to compile the Code using .Net 4.

I want to know that, are there anything in Catel framework that solves this kind of problem.

More Info:

For This Application The performance is the main issue, since I add many Items to the collection.

UPDATE:

using FastObservableCollection solves the problem, however the UI freezes for about 5-7 sec as soon as I get out of using. my guess is that, this is caused by the Dispatcher

I have manually overridden the OnCollectionChanged:

protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
  DispatcherHelper.CurrentDispatcher.BeginInvoke(new Action(() => base.OnCollectionChanged(e)),
              DispatcherPriority.ContextIdle);
}

this is not a good solution. is there a better way to avoid this problem?

Upvotes: 1

Views: 1008

Answers (1)

Geert van Horrik
Geert van Horrik

Reputation: 5724

You might consider using the FastObservableCollection in Catel:

using (fastCollection.SuspendChangeNotifications())
{
    // TODO: Add and remove all your items here
}

As soon as you get out of the using, it will pass it's change notifications

To get around the threading issues, you can use the DispatcherHelper.

Upvotes: 1

Related Questions