1

I have added 1 tab to tab control in design time in wpf .Now I want to add extra tabs to tabcontrol using MVVM pattern

Below is my code and what i have tried.

    <TabControl ItemsSource="{Binding ContainerTabItems, Mode=TwoWay}" SelectedIndex="{Binding WizardModel.TabIndexValue, Mode=TwoWay}" x:Name="tabControl1">
        <TabControl.Items>    
        <TabItem Header="Page Configuration">
                <Button Content="Search" x:Name="btnSearch" Command="{Binding SearchClick}" /> 
                </Grid>
        </TabItem>

        </TabControl.Items>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TabItem Header="{Binding Header}">
                    <TabItem.Content>
                        <ContentControl Content="{Binding UcContainer}"></ContentControl>
                    </TabItem.Content>
                </TabItem>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

    </TabControl>

When I click on btnSearch button I have to add new tab to tabcontrol using MVVM

void SearchClickCommand_Execute(object parameter)
{
    ContainerTabItems.Add(new ContainerTab()
             {
                   Header="Search",
                   UcContainer=new SearchGrid(newSearchContainer())
             });
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
Ravi Hanok
  • 405
  • 1
  • 12
  • 23

1 Answers1

0

Take a look at this answer. You basically create a collection of viewmodels, one for each tab, and bind it to TabControl's ItemSource property.

Community
  • 1
  • 1
Alex
  • 1,433
  • 9
  • 18
  • I can create dynamic tabs with empty tab control with data template but I want to add new tab to the tab control with existed tab at design time using mvvm,I hope requirement is clear – Ravi Hanok Jul 27 '16 at 10:22
  • Just simply set the itemsource for the one you already have in design time and create the dynamic ones on the fly – Sievajet Jul 27 '16 at 18:58