A true open source, API-first CMS — giving you the power to think outside the webpage. Try it for free.

Introduction

It is quite a common situation when UI shows a lot of different kinds of elements (text blocks, images, graphics, etc.) structured in different ways (lists, trees, etc.), but only one of these elements could be selected at the same time.

In this article, I will try to create a class which will help to deal with selection. WPF is used for the demo, but this approach can be used in UWP, Xamarin, Windows Forms, and maybe some other technologies.

ObservableCollection does not fire CollectionChanged with the list of removed (old) items after calling Clear(). It is possible to use ObservableCollection and not the Clear() method or to use ObservableCollectionEx to be able to use the Clear() method.

SelectionManager

AddCollection adds all elements in the collection to the internal list and searches for subelements using reflection (if some of the element properties implement ObservableCollection<> and elements of this collection implement ISelectableElement this collection also will be managed by SelectionManager).

RemoveCollection removes all elements and subelements from SelectionManager.