Esempio n. 1
0
        public void FocusNextTextBox(bool reverse = false)
        {
            var textBoxes = VisualTreeHelperEx.GetChildren(mUnitPriceItemsControl, true)
                            .OfType <TextBox>()
                            .Where(textbox => !textbox.IsReadOnly && textbox.IsEnabled);

            if (reverse)
            {
                textBoxes = textBoxes.Reverse();
            }

            TextBox first = null;
            var     lastTextBoxIsFocused = false;
            var     focusChanged         = false;

            foreach (var textBox in textBoxes)
            {
                if (first == null)
                {
                    first = textBox;
                }

                if (lastTextBoxIsFocused)
                {
                    textBox.Focus();
                    focusChanged = true;
                    break;
                }
                else
                {
                    lastTextBoxIsFocused = ReferenceEquals(FocusManager.GetFocusedElement(), textBox);
                }
            }

            if (!focusChanged && first != null)
            {
                first.Focus();
            }
        }