Dhinesh
Dhinesh

Reputation: 181

Unable to change back color of selected tabitem in silverlight

I have a TabControl with TabItem. I want to change the background colour of the tabitem header for the selected tab.

So I set the XAML code as below

<sdk:TabControl Background="WhiteSmoke" Foreground="Black" 
            SelectionChanged="TabControl_SelectionChanged">
<sdk:TabItem Name="adminTab" BorderBrush="Black">
    <sdk:TabItem.Header>
        <StackPanel Name="adminsp" Background="#C7CEF7">
            <Image Name="ico1" Source="Images/admin.png"/>
            <TextBlock Text="Admin"/>
        </StackPanel>
    </sdk:TabItem.Header>
</sdk:TabItem>
<sdk:TabItem Name="userTab" BorderBrush="Black">
    <sdk:TabItem.Header>
        <StackPanel Name="usersp" Background="#C7CEF7">
            <Image Name="ico1" Source="Images/user.png"/>
            <TextBlock Text="User"/>
        </StackPanel>
    </sdk:TabItem.Header>
</sdk:TabItem>

and in CS code as

void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    TabControl tab = sender as TabControl;
    if (adminTab.IsSelected)
    {
        adminsp.Background = new SolidColorBrush(Colors.Blue);  
    }
    else
    {
        adminsp.Background = new SolidColorBrush(Color.FromArgb(255, 199, 229, 249));
    }
                            .
                            .
}

But the background color is not changing, Any help would be appreciated!

Upvotes: 0

Views: 697

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222522

You should do something like this if you want to do it directway, otherwise you should Edit the style of TabControl,

    TabControl currentTab = (TabControl)sender;
    TabItem selectedItem = currentTab.SelectedItem as TabItem;
    if (selectedItem != null)
    {
        foreach (TabItem currentItem in currentTab.Items)
        {
            if (currentItem == selectedItem)
            {
                selectedItem.BorderBrush = new SolidColorBrush() { Color = Colors.Green };
                selectedItem.Background = new SolidColorBrush() { Color = Colors.LightGray };
            }
            else
            {
                currentItem.BorderBrush = new SolidColorBrush() { Color = Colors.Blue };
                currentItem.Background = new SolidColorBrush() { Color = Colors.Gray };
            }
        }
    }

Upvotes: 1

Related Questions