Beispiel #1
0
        /// <summary>
        /// A keyboard key has been issued in a parameter input field.
        /// </summary>
        ///
        /// If the key is returned the value in the field is stored back
        /// to the parameters data store, the input is disabled, and
        /// the label is shown with the new value.
        ///
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///
        private void ParameterEntry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return || e.Key == Key.Tab)
            {
                TextBox box = (TextBox)sender;
                e.Handled = true;

                int selectedIndex = ProgramParametersLB.SelectedIndex;

                try
                {
                    Parameters[selectedIndex].Value = box.Text;
                }
                catch (Exception /*ex*/)
                {
                    /* //TODO do something with the error */
                }
                RefreshCommandList();
                var moveDown = new TraversalRequest(FocusNavigationDirection.Down);
                //if it couldn't focus on the next element, we're at the end
                //seems like setting "Cycle" on the tab mode would solve this, but I'm doing it wrong or something in the xaml
                //hacky way of getting the first list box item, finding the parameter textbox and setting focus
                //using wpf dark magic
                if (!box.MoveFocus(moveDown))
                {
                    ListBoxItem firstItem = ProgramParametersLB.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
                    //FindChildControlByName is an extension method found in Utiity.cs class
                    TextBox firstParameterEntry = firstItem.FindChildControlByName <TextBox>("ParameterEntry");
                    firstParameterEntry.Focus();
                }
            }


            if (e.Key == Key.U)//(Keyboard.IsKeyDown(Key.LeftShift) && e.Key == Key.Up)
            {
                if (ProgramCommandsLB.SelectedIndex > 0)
                {
                    int     index = ProgramCommandsLB.SelectedIndex;
                    Command item  = Commands[index];
                    Commands.RemoveAt(index);
                    Commands.Insert(index - 1, item);
                    ProgramCommandsLB.SelectedIndex = index - 1;
                }
            }

            if (e.Key == Key.L)           //(Keyboard.IsKeyDown(Key.LeftShift) && e.Key == Key.Up)
            {
                if (ProgramCommandsLB.SelectedIndex < ProgramCommandsLB.Items.Count - 1)
                {
                    int     index = ProgramCommandsLB.SelectedIndex;
                    Command item  = Commands[index];
                    Commands.RemoveAt(index);
                    Commands.Insert(index + 1, item);
                    ProgramCommandsLB.SelectedIndex = index + 1;
                }
            }

            if (e.Key == Key.Delete || e.Key == Key.Back)
            {
                if (ProgramCommandsLB.SelectedIndex >= 0)
                {
                    int index = ProgramCommandsLB.SelectedIndex;
                    Commands.RemoveAt(index);
                    if (index < ProgramCommandsLB.Items.Count)
                    {
                        ProgramCommandsLB.SelectedIndex = index;
                    }
                    else if (ProgramCommandsLB.Items.Count > 0)
                    {
                        ProgramCommandsLB.SelectedIndex = ProgramCommandsLB.Items.Count - 1;
                    }
                }
            }
        }