public static int GetSelectedItemIndexAtPosition(ListBox listBox, GetPositionFromInputElementDelegate getPosition)
        {
            var index = -1;

            if (listBox?.Items != null)
            {
                for (int i = 0; i < listBox.Items.Count; ++i)
                {
                    if (listBox.SelectedItems.IndexOf(listBox.Items[i]) == -1)
                    {
                        continue;
                    }

                    var listViewItem = getListViewItem(listBox, i);

                    if (VisualTreeHelperTools.IsMouseOverTarget(listViewItem, getPosition))
                    {
                        index = i;
                        break;
                    }
                }
            }

            return(index);
        }
Exemple #2
0
        public static bool IsMouseOverTarget(Visual target, GetPositionFromInputElementDelegate getPosition)
        {
            // It can happen that target is null
            if (target == null)
            {
                return(false);
            }

            var bounds   = VisualTreeHelper.GetDescendantBounds(target);
            var mousePos = getPosition((IInputElement)target);

            return(bounds.Contains(mousePos));
        }
        /// <summary>
        /// The list will never be null but empty.
        /// </summary>
        public static List <T> GetSelectedItemsAtPosition <T>(ListBox listBox, GetPositionFromInputElementDelegate getPosition)
        {
            var selectedItems = new List <T>();

            var itemIndexAtPosition = GetSelectedItemIndexAtPosition(listBox, getPosition);

            // When mouse is over one item that is selected then we can return all selected items.
            if (itemIndexAtPosition >= 0)
            {
                selectedItems.AddRange(listBox.SelectedItems.OfType <T>());
            }

            return(selectedItems);
        }