WPF 4.5: Accessing bound collections on non UI threads

July 21, 2012

The single threaded behavior of WPF (and other UI technologies) requires that anything tied to the user interface be manipulated on the UI thread, incuding data bound objects. There are several ways to do that, assuming the code is on a non-UI thread, such as using Dispatcher.(Begin)Invoke, capturing and using the current SynchronizationContext, etc.Specifically, if some collection is data bound, items cannot be added or removed from it from a non-UI thread. WPF 4 (and earlier) throws an exception, because the data binding mechanism expects to be notified of changes (e.g. ObservableCollection<T>) on the UI thread.One of the improvements...
no comments

Windows 8 Metro: Detecting scroll changes in ListView

July 2, 2012

I had a requirement in a Metro app I’m working on to detect scrolling in a ListView (GridView is practically the same), or more precisely, detect whether the selected item goes off the visible ListView area, and if so, switch some items in the ListView so that the selected item be visible again; this is not an entirely accurate description, but it’s close enough for our purposes. An easy one, right?Searching the ListView class (and its bases) yields no useful results on scrolling. In WPF, the ScrollViewer element has an attached event, ScrollChanged. This can be used (in WPF)...