【问题标题】:Unable to change back color of selected tabitem in silverlight无法在 Silverlight 中更改所选 Tabitem 的背景颜色
【发布时间】:2014-05-15 01:28:03
【问题描述】:

我有一个TabControlTabItem。我想更改所选标签的tabitem 标头的背景颜色。

所以我将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>

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));
    }
                            .
                            .
}

但背景颜色没有改变,任何帮助将不胜感激!

【问题讨论】:

    标签: c# silverlight xaml tabcontrol tabitem


    【解决方案1】:

    如果你想直接这样做你应该这样做,否则你应该编辑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 };
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 2015-11-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多