简体   繁体   English

无法在Silverlight中更改所选Tabitem的背景色

[英]Unable to change back color of selected tabitem in silverlight

I have a TabControl with TabItem . 我有一个TabControlTabItem I want to change the background colour of the tabitem header for the selected tab. 我想更改所选标签的tabitem标头的背景颜色。

So I set the XAML code as below 所以我将XAML代码设置如下

<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 并在CS代码中为

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! 但是背景颜色没有改变,任何帮助将不胜感激!

You should do something like this if you want to do it directway, otherwise you should Edit the style of TabControl, 如果您想直接进行操作,则应执行以下操作,否则,应编辑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 };
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM