WPF TabIndex 不适用于 TabControl
WPF TabIndex not working for TabControl
我有一个带有 TabControl 的 window,在 TabControl 中有一个 TextBox。
TabOrder 没有按照我设置 TabIndexes 的方式执行它应该执行的操作。
它在 CheckBox 和 Button 之后进入 TabItem 和 TextBox。
<Grid HorizontalAlignment="Left" Height="140" Margin="10,570,0,0" VerticalAlignment="Top" Width="290">
<TextBlock x:Name="reasonBlock" HorizontalAlignment="Left" Margin="0,10,0,0" TextWrapping="Wrap" Text="Grund:" VerticalAlignment="Top" FontSize="14"/>
<TextBox x:Name="reasonBox" HorizontalAlignment="Left" Height="106" Margin="0,34,0,0" VerticalAlignment="Top" Width="290" AcceptsReturn="True" HorizontalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" MaxWidth="389" TextWrapping="Wrap" TabIndex="10"/>
</Grid>
<Grid x:Name="typeGrid" HorizontalAlignment="Left" Height="170" Margin="320,540,0,0" VerticalAlignment="Top" Width="389">
<TextBlock x:Name="typeBlock" HorizontalAlignment="Left" Margin="0,10,0,0" TextWrapping="Wrap" Text="Art der Änderung:" VerticalAlignment="Top" FontSize="14"/>
<TabControl x:Name="typeControl" HorizontalAlignment="Left" Height="136" Margin="0,34,0,0" VerticalAlignment="Top" Width="389">
<TabItem Header="Allgemein" TabIndex="19">
<Grid Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<TextBox x:Name="typeBox" HorizontalAlignment="Left" Height="101" VerticalAlignment="Top" Width="379" AcceptsReturn="True" HorizontalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" MaxWidth="379" TextWrapping="Wrap" TabIndex="20"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
<Grid HorizontalAlignment="Left" Height="95" Margin="714,615,0,0" VerticalAlignment="Top" Width="126">
<Button x:Name="createButton" Content="Erstellen" HorizontalAlignment="Right" Margin="0,70,0,0" VerticalAlignment="Top" Width="111" Click="createButton_Click" FontSize="14" BorderBrush="{DynamicResource ButtonBorderBrush}" TabIndex="101"/>
<CheckBox x:Name="publicBox" Content="Veröffentlichen" HorizontalAlignment="Right" Margin="0,31,0,0" VerticalAlignment="Top" TabIndex="100"/>
</Grid>
我通过订阅前一个 TextBox 的 KeyDown 事件修复了它,然后在按下 Tab 键时聚焦 TabItem。
private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Tab)
{
return;
}
else
{
TabItem tabItem = (TabItem)typeControl.SelectedItem;
tabItem.Focus();
e.Handled = true;
}
}
我有一个带有 TabControl 的 window,在 TabControl 中有一个 TextBox。
TabOrder 没有按照我设置 TabIndexes 的方式执行它应该执行的操作。
它在 CheckBox 和 Button 之后进入 TabItem 和 TextBox。
<Grid HorizontalAlignment="Left" Height="140" Margin="10,570,0,0" VerticalAlignment="Top" Width="290">
<TextBlock x:Name="reasonBlock" HorizontalAlignment="Left" Margin="0,10,0,0" TextWrapping="Wrap" Text="Grund:" VerticalAlignment="Top" FontSize="14"/>
<TextBox x:Name="reasonBox" HorizontalAlignment="Left" Height="106" Margin="0,34,0,0" VerticalAlignment="Top" Width="290" AcceptsReturn="True" HorizontalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" MaxWidth="389" TextWrapping="Wrap" TabIndex="10"/>
</Grid>
<Grid x:Name="typeGrid" HorizontalAlignment="Left" Height="170" Margin="320,540,0,0" VerticalAlignment="Top" Width="389">
<TextBlock x:Name="typeBlock" HorizontalAlignment="Left" Margin="0,10,0,0" TextWrapping="Wrap" Text="Art der Änderung:" VerticalAlignment="Top" FontSize="14"/>
<TabControl x:Name="typeControl" HorizontalAlignment="Left" Height="136" Margin="0,34,0,0" VerticalAlignment="Top" Width="389">
<TabItem Header="Allgemein" TabIndex="19">
<Grid Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<TextBox x:Name="typeBox" HorizontalAlignment="Left" Height="101" VerticalAlignment="Top" Width="379" AcceptsReturn="True" HorizontalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" MaxWidth="379" TextWrapping="Wrap" TabIndex="20"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
<Grid HorizontalAlignment="Left" Height="95" Margin="714,615,0,0" VerticalAlignment="Top" Width="126">
<Button x:Name="createButton" Content="Erstellen" HorizontalAlignment="Right" Margin="0,70,0,0" VerticalAlignment="Top" Width="111" Click="createButton_Click" FontSize="14" BorderBrush="{DynamicResource ButtonBorderBrush}" TabIndex="101"/>
<CheckBox x:Name="publicBox" Content="Veröffentlichen" HorizontalAlignment="Right" Margin="0,31,0,0" VerticalAlignment="Top" TabIndex="100"/>
</Grid>
我通过订阅前一个 TextBox 的 KeyDown 事件修复了它,然后在按下 Tab 键时聚焦 TabItem。
private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Tab)
{
return;
}
else
{
TabItem tabItem = (TabItem)typeControl.SelectedItem;
tabItem.Focus();
e.Handled = true;
}
}