0

Is there a way to initialize Tab content on Tab load? I'm having an issue, when Tab is created it doesnt initialize it components immediately. Only when I click on Tab the content will show up.

<TabControl
        ItemsSource="{Binding Items}">
                <TabControl.ItemTemplate>
                    <DataTemplate>
                        <DockPanel>
                            <TextBlock Text="{Binding TabName}"><TextBlock.Background><SolidColorBrush /></TextBlock.Background></TextBlock>
                            <Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0"  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}" BorderBrush="#00000000">
                                <Image Source="/WPF_AccApp;component/Images/11.gif" Height="11" Width="11"></Image>
                            </Button>
                            <DockPanel.Background>
                                <SolidColorBrush />
                            </DockPanel.Background>
                        </DockPanel>
                    </DataTemplate>
                </TabControl.ItemTemplate>
                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <views:TabItemView />
                    </DataTemplate>
                </TabControl.ContentTemplate>
            </TabControl>

Button click event

        private void AddInvoice_Click(object sender, RoutedEventArgs e)
    {
        count++;

        string s = string.Format("Tab {0}", count);
        mainViewModel.Items.Add(new ItemViewModel(s));
        this.DataContext = mainViewModel;
        if (count == 1)
        {
           //this is as work around
           // mainViewModel.Items.Add(new ItemViewModel(s));
           // mainViewModel.Items.RemoveAt(1);
        }

    }
Mrg Gek
  • 906
  • 2
  • 10
  • 31

1 Answers1

0

Found solution https://social.msdn.microsoft.com/Forums/vstudio/en-US/8043441a-95bb-4f70-b64c-a76f89980068/tabcontrol-getting-the-work-done-in-viewmodel-when-selectionchanged?forum=wpf

You need to add Selected Tab Index property and bind it to TabControl

Mrg Gek
  • 906
  • 2
  • 10
  • 31