Пример #1
0
        private void Row_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            var hitbox = (FrameworkElement)sender;
            var item   = (FileSystemElement)hitbox.Tag;

            ItemDoubleTapped?.Invoke(this, item);
        }
Пример #2
0
        private void Window_KeyDown(object sender, KeyRoutedEventArgs args)
        {
            if (ItemsSource.Count == 0)
            {
                return;
            }

            var shiftDown = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
            var ctrlDown  = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);

            switch (args.Key)
            {
            //Abort selection
            case VirtualKey.Escape:
                UnselectOldRows();
                break;

            //Select currently focused item
            case VirtualKey.Space:
                if (shiftDown)
                {
                    SelectRowsBetween(FocusedItem);
                }
                else
                {
                    ToggleSelect(FocusedItem);
                }
                break;

            //Open/Navigate currently focused item
            case VirtualKey.Enter:
                ItemDoubleTapped?.Invoke(this, FocusedItem);
                break;

            case VirtualKey.Menu:
                if (focusedRow != null)
                {
                    OpenItemFlyout(FocusedItem, focusedRow, new Point(0, focusedRow.Height));
                }
                break;

            //Move focus to next/previous element and keep it in view
            case VirtualKey.Up:
            case VirtualKey.Down:
            case VirtualKey.W:
            case VirtualKey.S:
                //If there is a focused item take index of it
                //If there is no focused item and no SelectedItems begin from start
                //If there is no focused item but SelectedItems get the index of the last
                var lastFocusedIndex = FocusedItem == null ?
                                       SelectedItems.Count == 0 ? -1 : ItemsSource.IndexOf(SelectedItems.Last())
                        : ItemsSource.IndexOf(FocusedItem);

                var up = args.Key == VirtualKey.Up || args.Key == VirtualKey.W;

                var index         = up ? lastFocusedIndex - 1 : lastFocusedIndex + 1;              //Find next index depending on which button has been pressed
                var condBoundings = up ? index >= 0 : index < ItemsSource.Count;                   //Check bounding 0 <= index < ItemsSource.Count

                if (!condBoundings)
                {
                    break;                                                      //Cancel move focus if next item would be out of bounds
                }
                if (ctrlDown)
                {
                    //Select next row if ctrl was pressed
                    ToggleSelect(ItemsSource[index]);

                    //Select LastFocusedItem if its not selected
                    if (!SelectedItems.Contains(ItemsSource[lastFocusedIndex]))
                    {
                        ToggleSelect(ItemsSource[lastFocusedIndex]);
                    }
                }

                FocusRow(ItemsSource[index], true);

                args.Handled = true;                                        //Set event as handled (Prevents ScrollViewer from scrolling down/up)
                break;
            }

            //Don't find FileSystemElemets which start with key pressed if modifiers have been pressed
            if (ctrlDown || shiftDown)
            {
                return;
            }

            var focusItemIndex = ItemsSource.IndexOf(FocusedItem);
            var key            = args.Key.ToString().ToLower();
            var firstItemIndex = -1;
            var foundNextItem  = false;

            for (int i = 0; i < ItemsSource.Count; i++)
            {
                if (ItemsSource[i].Name.ToLower().StartsWith(key))
                {
                    //To begin from first item again if last item with the key was found
                    if (firstItemIndex == -1)
                    {
                        firstItemIndex = i;
                    }

                    //Needs to be after currently focused item
                    if (i <= focusItemIndex)
                    {
                        continue;
                    }

                    UnselectOldRows();
                    TryScrollFocusSelect(ItemsSource[i]);
                    foundNextItem = true;
                    break;
                }
            }

            if (!foundNextItem && firstItemIndex != -1)
            {
                UnselectOldRows();
                TryScrollFocusSelect(ItemsSource[firstItemIndex]);
            }
        }