Open inosik opened 4 years ago
Thanks, @inosik. We will take a look.
In order to make it work, you need to make some changes in the code. In MainWindow.xaml
while defining TabControl
, add Loaded
event as Loaded="TabControl_Loaded"
, find code snippet below :
<TabControl>
<TabItem Header="Repro">
<TabControl
Margin="10"
ItemsSource="{Binding Tabs}"
SelectedItem="{Binding SelectedTab}" Loaded="TabControl_Loaded">
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
<Setter Property="Header" Value="{Binding Header}" />
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</TabItem>
</TabControl>
And in MainWindow.xaml.cs
add the following code -
private void TabControl_Loaded(object sender, RoutedEventArgs e)
{
var tabControl = (TabControl)sender;
tabControl.SelectedIndex = 0;
}
Here you can provide any tab index.
@anjali-wpf Thanks for looking at this. Yes, this does work. As well as setting the view models SelectedTab
property when constructing it. This still looks like a bug to me, because it works without any extra code when the "Repro" tab becomes visible later.
Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
Problem description:
Tab controls with data-bound
ItemsSource
don't select an initial tab item, if the tab control is visible at program start. If the tab control becomes visible later, it properly selects the first tab item. This can be verified by moving the "Dummy" tab item from the repro sample before the "Repro" tab item.Actual behavior:
Expected behavior:
The inner tab control should automatically select the first item.
Minimal repro:
Tabs.zip