WPF tab order working wrong

后端 未结 2 1827
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 01:04

I have a view in WPF that I have been fighting to get the tab order correct on. I have three text boxes (lets call them Text1, Text2, and Text3) and two custom controls, eac

相关标签:
2条回答
  • 2021-01-20 01:29

    I don't have your custom control to hand, so I created one that contained just a TextBox. After wiring this up to your XAML, I found that the tab order went as follows:

    TextBox1, TextBox2, CustomControl1, CustomControl2, TextBox3, TextBox within CustomControl1, TextBox within CustomControl2.

    Note that after TextBox2 tabbing moves the focus onto the custom controls themselves, not any child controls they happen to have. The TextBox in my custom control didn't have a TabIndex, so its TabIndex was assumed to be the default value, Int32.MaxValue (see the MSDN documentation for the TabIndex property). Hence they come last in the tab order.

    I found that things worked better if I marked the custom controls as IsTabStop="False" (I don't see why I'd want the focus on the custom controls themselves), and set the tab-index of the TextBox in the custom control to that given in the <my:CustomControl /> element by adding the attribute TabIndex="{TemplateBinding TabIndex}" to the TextBox in the custom control. Once I'd done that, the tab order was, as I would expect,

    TextBox1, TextBox2, TextBox within CustomControl1, TextBox within CustomControl2, TextBox3.

    Of course, my custom control consists only of a single TextBox, so setting a single tab-index fixed things for me. I don't have your code, so I can't say for sure, but it might be enough to set the tab-index of all child controls within your custom controls to that in the <my:CustomControl /> element in the same way.

    EDIT: if you can't use a TemplateBinding, and instead have a .xaml file with a corresponding .xaml.cs file, then you have what's called a user control, not a custom control. In that case, you can try setting the tab index inside the XAML for CustomControlA using something like the following:

    TabIndex="{Binding Path=TabIndex, RelativeSource={RelativeSource AncestorType={x:Type my:CustomControlA}}}"
    
    0 讨论(0)
  • 2021-01-20 01:34

    Please post your XAML code. In the meantime, some things to consider include:

    • Check the KeyboardNavigation.TabIndex attribute values one more time
    • Are you dynamically adding the custom tabs at runtime?
    • Are you programmatically altering or interacting with the custom controls at runtime?
    • Ensure the tabs are listed in the proper order, in the XAML markup
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题