简体   繁体   English

当Tab控件中的selectedindex选项卡更改时,如何自动移动焦点?

[英]How to automatically move focus when selectedindex tab change in Tab control?

I have Tabcontrol like this. 我有这样的Tabcontrol

<TabControl SelectedIndex="{Binding SelectedTab}" Name="TabControlAll">
        <TabItem Header="Hal 1" Style="{StaticResource AzureTabItem}">
            <kor:Halaman1 DataContext="{Binding RTDetail}"/>
        </TabItem>
        <TabItem Header="Hal 2" Style="{StaticResource AzureTabItem}" >
            <kor:Halaman2 />
        </TabItem>
        <TabItem Header="Hal 3" Style="{StaticResource AzureTabItem}">
            <kor:Halaman3/>
        </TabItem>
        <TabItem Header="Hal 4" Style="{StaticResource AzureTabItem}">
            <kor:Halaman4/>
        ...

</TabControl>

And the view can be like this. 而且视图可以像这样。 的tabcontrol

In my UserControl I have so many controls like Hal 12 with many Textboxes inside Usercontrol Halaman so what I wanna ask How I can automatically change keyboard focus on change Tabcontrol index to first Textbox in selected tab? 在我的UserControl我有很多控件,例如Hal 12,在Usercontrol Halaman中带有许多Textboxes所以我想问一下如何自动将键盘焦点更改为将Tabcontrol索引更改为所选标签中的第一个Textbox

And I have to move also to next TabControl when focus of keyboard has reached the last control in that page. 当键盘焦点到达该页面的最后一个TabControl时,我还必须移至下一个TabControl

Any body can help? 任何机构都能帮忙吗?

This event handler will move the focus to the next TabItem when tab key is pressed. 按下Tab键时,此事件处理程序会将焦点移至下一个TabItem。

private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if(e.Key == Key.Tab)
        tabControl.SelectedIndex = (tabControl.SelectedIndex + 1) % tabCount;
}

Upon moving the focus, you can set this event handler to last control in your tab item. 移动焦点后,可以将此事件处理程序设置为选项卡项中的最后一个控件。 This will focus the first control in the selected TabItem. 这将使第一个控件集中在选定的TabItem中。 You can replace element with the name of your first control. 您可以将元素替换为第一个控件的名称。

private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    element.Dispatcher.BeginInvoke(new Action<UIElement>(x =>
    {
        x.Focus();
    }), DispatcherPriority.ApplicationIdle, element);
}

This should be working fine. 这应该工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM