Kurtis Aung
Kurtis Aung

Reputation: 282

Best Way to Replace UserControl Loaded in TabControl TabItem

I currently have a TabControl loaded in MainWindow.xaml, which has three tabs. Let's name them Tab1, Tab2 and Tab3.

The individual tabs have views (usercontrols) supplied and they all work as expected. Code as follows. I am using MahApps Metro TabControl here.

<Controls:MetroAnimatedSingleRowTabControl Grid.Row="0" Grid.ColumnSpan="4" x:Name="MainTabControl">
            <TabItem Header="Tab1">
                <view:Tab1View DataContext="{Binding}"  />
            </TabItem>
            <TabItem Header="Tab2">
                <view:Tab2View DataContext="{Binding}" />
            </TabItem>
            <TabItem Header="Tab3">                    
                <view:Tab3View DataContext="{Binding}" />
            </TabItem>                
        </Controls:MetroAnimatedSingleRowTabControl>

What I would like to do now is to switch the view of Tab3 (which is Tab3View.xaml) to another view (let's call it subTab3View.xaml) when I click on a button on Tab3View.xaml. This will basically switch the Tab3 content from Tab3View.xaml to subTab3View.xaml.

Could anyone kindly suggest me a way to achieve this?

Upvotes: 2

Views: 953

Answers (1)

Muds
Muds

Reputation: 4116

You can have a content control in your tab, and then change the content on button click or whatever event you want.

<TabItem Header="Tab3">                    
    <ContentControl x:Name="contentControl"/>
</TabItem>                


private void ButtonClick(object sender, RoutedEventArgs e)
{
    this.contentControl.Content = new Tab3View();
}

Upvotes: 1

Related Questions