CBreeze
CBreeze

Reputation: 2983

TabItem Selected not Working Properly

I have a TabControl that contains three TabItems. I have a label on each TabItem so I can program the click event, it looks like this;

<TabItem.Header>
    <Label Content="People" HorizontalAlignment="Stretch" FontSize="14" MouseLeftButtonDown="TabItemClick"/>
</TabItem.Header>

I want to program the TabItem click so that something happens depending on which TabItem is selected, so I wrote something like this;

private void TabItemClick(object sender, MouseButtonEventArgs e)
{
    var privilegeService = new PrivilegeService();
    if (companyTabItem.IsSelected == true)
    {
        generateCompanyPrivilegesButton.IsEnabled = true;
        applyCompanyPrivilegesButton.IsEnabled = false;
    }
    else if (peopleTabItem.IsSelected == true)
    {
        MessageBox.Show("People Selected");
        generatePeoplePrivilegesButton.IsEnabled = false;
        applyPeoplePrivilegesButton.IsEnabled = false;
    }
    else if (contractTabItem.IsSelected == true)
    {
        generateContractsPrivilegesButton.IsEnabled = false;
        applyContractsPrivilegesButton.IsEnabled = false;
    }
}

As you can see I added in a MessageBox for some testing purposes to see when a TabItem is selected. The MessageBox is only displayed however after I LEAVE the peopleTabItem, not when I click onto it. How can I change this so that I can program someting that happens when the TabItem is clicked AND when it is selected too?

Upvotes: 0

Views: 223

Answers (1)

Giangregorio
Giangregorio

Reputation: 1510

I will use a different approach. Yo ucan use TabControl void SelectionChanged event of yout TabControl like this:

XAML:

<TabControl x:Name="testTab" SelectionChanged="TabControl_SelectionChanged">

Code behind:

void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  // you can use testTab.SelectedItem here or youtTab.IsSelected
}

Upvotes: 2

Related Questions