I need to select several tabs programatically with delay on button click. I looked through this answer, but it works only if I select a single tab without any extra code. In my case, I need to cycle through the tabs so user can see them in process:
private void button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 10; i++)
{
Dispatcher.BeginInvoke((Action)(() => tabControl.SelectedIndex = i % 3));
Thread.Sleep(1000);
}
}
<TabControl x:Name="tabControl">
<TabItem Header="TabItem1" />
<TabItem Header="TabItem2" />
<TabItem Header="TabItem3">
<Grid>
<Button x:Name="button"
Content="Click"
Height="200"
Width="200"
Click="button_Click" />
</Grid>
</TabItem>
</TabControl>
However, the UI is blocked for the whole cycle and only the last tab is shown after the cycle is complete. How to solve the problem?
private async void button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 10; i++)
{
tabControl.SelectedIndex = i % 3;
await Task.Delay(1000);
}
}