Un-Known
Un-Known

Reputation: 43

Go to previous Tab using button in WPF

How to go previous tab if I click back button? XAML

<Button x:Name="btnBack" Margin="239,418,333,11 Click="btnBack_Click">
    <Button.Template>         
        <ControlTemplate>                    
            <Image x:Name="backImg" Source="Images/back.png" />                
        </ControlTemplate>           
    </Button.Template>        
</Button>

Code

private void btnBack_Click(object sender, RoutedEventArgs e)     
{
}

Tab item

<TabControl HorizontalAlignment="Left" Height="403" BorderThickness="0" 
            Background="Transparent" Margin="10,10,0,0"
            VerticalAlignment="Top" Width="622">       
    <TabItem Header="List" Margin="0,0,-19,0">     
        //object    
    </TabItem>        
    <TabItem Header="Register" Margin="0,0,-19,0">    
    </TabItem>    
    //object    
</TabControl>

It is possible to do that?

Upvotes: 1

Views: 1055

Answers (1)

AzzamAziz
AzzamAziz

Reputation: 2171

In the TabControl object you have a SelectedIndex that you can use to navigate to any element within the list. Try something like the following:

private void btnBack_Click(object sender, RoutedEventArgs e)     
{
    if (tabControl.SelectedIndex == 0)
    {
        return;
    }
    else
    {
        tabControl.SelectedIndex -= 1;
    }
}

In this case we are calling the tab control, checking if we can reverse in the items it has and if we can we will go backward by 1 item.

Keeping in mind that your XAML TabControl needs to be given a name to be called by from the code behind:

<TabControl x:Name="tabControl">

Upvotes: 6

Related Questions