WPF tab order with custom controls?

前端 未结 5 1247
别那么骄傲
别那么骄傲 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:47

    I have an answer to the first one... In your static constructor for your CustomControl add the following

    KeyboardNavigation.TabNavigationProperty.OverrideMetadata(typeof(NumericSpinner), new FrameworkPropertyMetadata(KeyboardNavigationMode.Local));
    

    That should let you tab in and out of the control and allow you to set a tab index for each of the children of your custom control.

    As for your second question, I'm working on figuring out the same thing. I think it has to due with the fact that your custom control has Focusable = False. But setting this to true will make the control get focus not the actual children. I think what might be required is an event handler on your CustomControl for the GotFocus event. When the control gets focus then do a

    this.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
    

    and that should move the focus to the child elements.

    I'll post a follow up when I figure out the proper way of doing it.

    Update

    So for your second point.

    Make sure you set focusable to false and the TabNavigationProperty like so in the static constructor for your control

    FocusableProperty.OverrideMetadata(typeof(NumericSpinner), new FrameworkPropertyMetadata(false));
    KeyboardNavigation.TabNavigationProperty.OverrideMetadata(typeof(NumericSpinner), new FrameworkPropertyMetadata(KeyboardNavigationMode.Local));
    

    This will allow the control to work as expected and respect the Tab Order on the control and also all the children. Make sure that in your style you don't set the Focusable or TabNavigation properties.

    Raul

提交回复
热议问题