Example #1
0
        protected override void OnSelected(RoutedEventArgs e)
        {
            base.OnSelected(e);
            RequestBringIntoView += TreeViewItemEx_RequestBringIntoView;

            if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
            {
                ItemsControl items = ParentItemsControl;

                bool hasMultipleSelected = false;
                foreach (object item in items.Items)
                {
                    TreeViewItemEx ex = items.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItemEx;
                    if (ex != null &&
                        ex.IsMultipleSelected)
                    {
                        hasMultipleSelected = true;
                        break;
                    }
                }
                if (hasMultipleSelected)
                {
                    ParentTreeView.HandlingMultipleSelected = true;
                    bool select = false;
                    foreach (object item in items.Items)
                    {
                        TreeViewItemEx ex = items.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItemEx;
                        if (ex == null)
                        {
                            continue;
                        }
                        if (select)
                        {
                            if (ReferenceEquals(ex, this) ||
                                ex.IsMultipleSelected)
                            {
                                ex.IsMultipleSelected = true;
                                break;
                            }
                            ex.IsMultipleSelected = true;
                        }
                        else
                        {
                            if (ReferenceEquals(ex, this) ||
                                ex.IsMultipleSelected)
                            {
                                ex.IsMultipleSelected = true;
                                select = true;
                            }
                        }
                    }
                }
            }
            else
            {
                ParentTreeView?.ClearMultipleSelection();
                IsMultipleSelected = true;
                if (ParentTreeView != null)
                {
                    ParentTreeView.HandlingMultipleSelected = true;
                }
            }
        }