示例#1
0
        /// <summary>
        ///     A KeyHandler method used by SearchView, increments decrements and executes based on input.
        /// </summary>
        /// <param name="sender">Originating object for the KeyHandler </param>
        /// <param name="e">Parameters describing the key push</param>
        public void KeyHandler(object sender, KeyEventArgs e)
        {
            // ignore the key command if modifiers are present
            if (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) ||
                e.KeyboardDevice.IsKeyDown(Key.RightCtrl) ||
                e.KeyboardDevice.IsKeyDown(Key.LeftAlt) ||
                e.KeyboardDevice.IsKeyDown(Key.RightAlt))
            {
                return;
            }

            switch (e.Key)
            {
            case Key.Return:
                viewModel.Execute();
                break;

            case Key.Delete:
                if (DynamoSelection.Instance.Selection.Count > 0)
                {
                    e.Handled = true;
                    this.dynamoViewModel.DeleteCommand.Execute(null);
                }

                //if there are no nodes being selected, the delete key should
                //delete the text in the search box of library preview
                else
                {
                    //if there is no text, then jump out of the switch
                    if (String.IsNullOrEmpty(SearchTextBox.Text))
                    {
                        break;
                    }
                    else
                    {
                        int    cursorPosition = SearchTextBox.SelectionStart;
                        string searchBoxText  = SearchTextBox.Text;

                        //if some piece of text is seleceted by users.
                        //delete this piece of text
                        if (SearchTextBox.SelectedText != "")
                        {
                            searchBoxText = searchBoxText.Remove(cursorPosition,
                                                                 SearchTextBox.SelectionLength);
                        }

                        //if there is no text selected, delete the character after the cursor
                        else
                        {
                            //the cursor is at the end of this text string
                            if (cursorPosition == searchBoxText.Length)
                            {
                                break;
                            }
                            else
                            {
                                searchBoxText = searchBoxText.Remove(cursorPosition, 1);
                            }
                        }

                        //update the SearchTextBox's text and the cursor position
                        SearchTextBox.Text           = searchBoxText;
                        SearchTextBox.SelectionStart = cursorPosition;
                    }
                }
                break;

            case Key.Tab:
                viewModel.PopulateSearchTextWithSelectedResult();
                break;

            case Key.Down:
                viewModel.SelectNext();
                break;

            case Key.Up:
                viewModel.SelectPrevious();
                break;
            }
        }