private List <MultiSelectTreeViewItem> GetTreeViewItemRange(
            MultiSelectTreeViewItem start,
            MultiSelectTreeViewItem end)
        {
            List <MultiSelectTreeViewItem> items = GetTreeViewItems(this, false);

            int startIndex = items.IndexOf(start);
            int endIndex   = items.IndexOf(end);
            int rangeStart = startIndex > endIndex || startIndex == -1 ? endIndex : startIndex;
            int rangeCount = startIndex > endIndex ? startIndex - endIndex + 1 : endIndex - startIndex + 1;

            if (startIndex == -1 && endIndex == -1)
            {
                rangeCount = 0;
            }
            else if (startIndex == -1 || endIndex == -1)
            {
                rangeCount = 1;
            }

            return(rangeCount > 0 ? items.GetRange(rangeStart, rangeCount) : new List <MultiSelectTreeViewItem>());
        }
        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseUp(e);

            // If clicking on a tree branch expander...
            if (InExpanderTree(e.OriginalSource))
            {
                return;
            }

            MultiSelectTreeViewItem item = GetTreeViewItemClicked((FrameworkElement)e.OriginalSource);

            if (item != null)
            {
                if (e.ChangedButton == MouseButton.Left && item == mouseDownItem)
                {
                    SelectedItemChangedInternal(item);

                    item.IsSelected = true;
                }
            }
        }
        private void SelectedItemChangedInternal(MultiSelectTreeViewItem tvItem)
        {
            // Clear all previous selected item states if ctrl is NOT being held down
            if (!IsCtrlPressed)
            {
                List <MultiSelectTreeViewItem> items = GetTreeViewItems(this, true);
                foreach (MultiSelectTreeViewItem treeViewItem in items)
                {
                    SetIsItemSelected(treeViewItem, false);
                }
            }

            // Is this an item range selection?
            if (IsShiftPressed)
            {
                if (_lastItemSelected == null)
                {
                    // Otherwise, individual selection
                    SetIsItemSelected(tvItem, true);
                    _lastItemSelected = tvItem;
                }
                else
                {
                    List <MultiSelectTreeViewItem> items = GetTreeViewItemRange(_lastItemSelected, tvItem);
                    if (items.Count > 0)
                    {
                        foreach (MultiSelectTreeViewItem treeViewItem in items)
                        {
                            if (OneSelectedItem == null ||
                                treeViewItem.Header.GetType() == OneSelectedItem.Header.GetType())
                            {
                                SetIsItemSelected(treeViewItem, true);
                            }
                        }

                        _lastItemSelected = items.Last();
                    }
                }
            }
            else if (IsCtrlPressed)
            {
                //Deselect if it's selected
                if (GetIsItemSelected(tvItem))
                {
                    SetIsItemSelected(tvItem, false);
                    _lastItemSelected = tvItem;
                }
                else if (OneSelectedItem == null ||
                         OneSelectedItem.Header.GetType() == tvItem.Header.GetType())
                {
                    //Select it if it's on its own, or matches the tier of the other selected items
                    SetIsItemSelected(tvItem, true);
                    _lastItemSelected = tvItem;
                }
            }
            else
            {
                // Otherwise, individual selection
                SetIsItemSelected(tvItem, true);
                _lastItemSelected = tvItem;
            }
        }