private void DeselectPickerClassItem(DeckPickerClassItem dpci)
        {
            var heroClass = dpci.DataContext as HeroClassAll?;

            if (heroClass != null && SelectedClasses.Remove(heroClass.Value))
            {
                dpci.OnDelselected();
            }
        }
        private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_reselectingClasses)
            {
                return;
            }

            IEnumerable <DeckPickerClassItem> removedPickerClassItems;
            var addedPickerClassItems = e.AddedItems.OfType <DeckPickerClassItem>();
            var addedClasses          = PickerClassItemsAsEnum(addedPickerClassItems);

            if (addedClasses.Contains(HeroClassAll.All))
            {
                _reselectingClasses = true;
                var senderList = ((ListView)sender);
                senderList.UnselectAll();
                foreach (var item in senderList.Items)
                {
                    var dpci = item as DeckPickerClassItem;
                    if (dpci != null)
                    {
                        var hca = (HeroClassAll)dpci.DataContext;

                        switch (hca)
                        {
                        case HeroClassAll.All:
                            senderList.SelectedItems.Add(item);
                            SelectPickerClassItem(dpci);
                            break;

                        case HeroClassAll.Archived:
                            if (!SelectedClasses.Contains(HeroClassAll.Archived))
                            {
                                if (addedClasses.Contains(HeroClassAll.Archived))
                                {
                                    senderList.SelectedItems.Add(item);
                                    SelectPickerClassItem(dpci);
                                }
                            }
                            else
                            {
                                removedPickerClassItems = e.RemovedItems.OfType <DeckPickerClassItem>();
                                if (PickerClassItemsAsEnum(removedPickerClassItems).Contains(HeroClassAll.Archived))
                                {
                                    DeselectPickerClassItem(dpci);
                                }
                                else
                                {
                                    senderList.SelectedItems.Add(item);
                                }
                            }
                            break;

                        default:
                            DeselectPickerClassItem(dpci);
                            break;
                        }
                    }
                }
                _reselectingClasses = false;
            }
            else
            {
                DeckPickerClassItem removedAllClassItem = null;
                removedPickerClassItems = e.RemovedItems.OfType <DeckPickerClassItem>();
                foreach (var dpci in removedPickerClassItems)
                {
                    var heroClass = dpci.DataContext as HeroClassAll?;
                    if (heroClass == null)
                    {
                        continue;
                    }

                    if (heroClass == HeroClassAll.All)
                    {
                        // We remove this from SelectedClasses now but we don't raise it's OnDeselected event yet,
                        // instead store a reference to it in case we want to quietly add this back to the
                        // SelectedClasses list later
                        SelectedClasses.Remove(heroClass.Value);
                        removedAllClassItem = dpci;
                    }
                    else
                    {
                        DeselectPickerClassItem(dpci);
                    }
                }

                var allIsSelected = SelectedClasses.Contains(HeroClassAll.All);
                foreach (var dpci in addedPickerClassItems)
                {
                    var heroClass = dpci.DataContext as HeroClassAll?;
                    if (heroClass == null)
                    {
                        continue;
                    }

                    if (allIsSelected && heroClass != HeroClassAll.Archived)
                    {
                        _reselectingClasses = true;
                        ((ListView)sender).SelectedItems.Remove(dpci);
                        _reselectingClasses = false;
                        continue;
                    }

                    SelectPickerClassItem(dpci);
                }

                if (SelectedClasses.Count == 0 && !_clearingClasses)
                {
                    var senderList = (ListView)sender;
                    if (removedAllClassItem == null)
                    {
                        var dpciAll = PickerClassItemFromEnum(senderList, HeroClassAll.All);

                        // Select 'All', raising its OnSelected event
                        _reselectingClasses = true;
                        senderList.SelectedItems.Add(dpciAll);
                        SelectPickerClassItem(dpciAll);
                        _reselectingClasses = false;
                    }
                    else
                    {
                        // If there are no selected classes, and we earlier removed 'All', quietly add it back
                        _reselectingClasses = true;
                        senderList.SelectedItems.Add(removedAllClassItem);
                        SelectedClasses.Add(HeroClassAll.All);
                        _reselectingClasses = false;

                        // And make sure we do not raise its OnDeselected event if we were going to
                        removedAllClassItem = null;
                    }
                }

                // If we removed the 'All' class earlier, raise the DeckPickerClassItem's OnDeselected event now
                if (removedAllClassItem != null)
                {
                    removedAllClassItem.OnDelselected();
                }
            }

            if (Helper.MainWindow.IsLoaded)
            {
                UpdateDecks();
            }
        }