WPF tab order with custom controls?

前端 未结 5 1214
别那么骄傲
别那么骄傲 2021-01-14 13:46

I have a WPF page that contains several out of the box controls with the tab order set.

I have a custom control (NumericSpinner) that contains: border/grid/text box/

5条回答
  •  余生分开走
    2021-01-14 13:46

    Raul answered the primary issue - being able to tab into the text box of a custom control. The secondary problem was not being able to tab out of the text box.

    The issue with this control was that its textbox contains a keypress handler:

    PreviewKeyDown="valueText_PreviewKeyDown"
    

    The handler only allows certain keys to be pressed within the textbox:

    /// 
    /// Since this event handler traps keystrokes within the control, in order to facilitate tabbing order, allowing the 
    /// tab key press must be enabled
    /// 
    /// 
    /// 
    private void valueText_PreviewKeyDown(object sender, KeyEventArgs e)
    { 
        KeyConverter converter = new KeyConverter();
        string key = converter.ConvertToString(e.Key);
        int index = ((TextBox)sender).CaretIndex;
        if (key != null)
        {
            if (AllowNegativeValues && (e.Key == Key.Subtract || e.Key == Key.OemMinus))
            {
                e.Handled = (valueText.Text.Contains('-') || index > 0) == true;
            }
            else if (AllowDecimal && (e.Key == Key.OemPeriod || e.Key == Key.Decimal))
            {
                e.Handled = valueText.Text.Contains('.') == true;
            }
            else
                e.Handled = ((((e.Key >= Key.D0) && (e.Key <= Key.D9) && (e.KeyboardDevice.Modifiers != ModifierKeys.Shift))
                                || ((e.Key >= Key.NumPad0) && (e.Key <= Key.NumPad9) && (e.KeyboardDevice.Modifiers != ModifierKeys.Shift))
                                || e.Key == Key.Left || e.Key == Key.Right
                                || e.Key == Key.Back || e.Key == Key.Delete 
                                || e.Key == Key.Tab) == false);             
        }
        else
            e.Handled = true;
    }
    

    I simply had to add the allowable TAB keystroke and the custom control works fine:

    e.Key == Key.Tab
    

提交回复
热议问题