This article presents the idea and implementation of a class to manage the selection of only one element in different linear and hierarchical structures.
Introduction
It is a quite common situation when the 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.
To be handled by Selection Manager, the object should implement ISelectableElement interface.
PropertyHelper is used to get the property name.
AddCollection adds all elements in the collection to the internal list and searchs for subelements using reflection (if some of the element properties implement ObservableCollection<> and elements of this collection implement ISelectableElement this collection will also be managed by SelectionManager).
RemoveCollection removes all elements and subelements from SelectionManager.
SelectionManager will handle adding and removing subelements automatically.
This demo application contains a list and a tree. Selection is managed by SelectionManager.
MVVM Light framework is used to make the code more compact and clear.
There are two types of the objects which support selection.
Pro WPF: Windows Presentation Foundation in .NET 3.0