theSpyCry
theSpyCry

Reputation: 12283

WPF ICollectionView Refresh

Is there any way how to do

ICollectionView.Refresh() 

or

CollectionViewSource.GetDefaultView(args.NewValue).Refresh();

in a separate thread?

I know I can use dispatcher, but this collection is binded to a ListView and it throws cross thread Exceptions.

The reason why I need a second thread is, that I have Control which displays a list of IMyItems. When filtering this Collection (by user text change input), I want to be able to display my animation that CollectionView is changing.

Upvotes: 1

Views: 7116

Answers (3)

Jeff Wilcox
Jeff Wilcox

Reputation: 6385

You can't!

All UI operations must happen on the user interface thread, and nearly every call inside of WPF's DispatcherObject (and all controls in that hierarchy) are regularly going to be calling CheckAccess().

You might want to consider using an ObservableCollection to help keep your data up-to-date, if you're doing processing in a background thread or BackgroundWorker.

Upvotes: 3

Arcturus
Arcturus

Reputation: 27065

How about using the Dispatcher to do work with background priority?

Dispatcher.Invoke(DispatcherPriority.Background,
    () => { CollectionViewSource.GetDefaultView(args.NewValue).Refresh(); }
);

Upvotes: 3

Kenan E. K.
Kenan E. K.

Reputation: 14111

I hacked up a quick method to invoke actions on wpf dispatchable objects (all wpf controls inherit from DispatcherObject)

public static void InvokeWpf(DispatcherObject dispatchable, Action action, bool async)
{
    // DispatcherOperationCallback is optimized for wpf invoke calls
    DispatcherOperationCallback toDo = delegate{ action(); return null; };

    if (!dispatchable.CheckAccess())
    {
        if (async) 
            dispatchable.Dispatcher.BeginInvoke(toDo, null);
        else
            dispatchable.Dispatcher.Invoke(toDo, null);
    }
    else
    {
        toDo(null);
    }
}

Usage:

InvokeWpf(listView, 
       () => CollectionViewSource.GetDefaultView(listView).Refresh(), 
       false);

Upvotes: -2

Related Questions