无法在 silverlight 中更改所选选项卡项的返回颜色
本文关键字:选项 颜色 返回 silverlight | 更新日期: 2023-09-27 17:56:26
我有一个TabControl
TabItem
.我想更改所选选项卡的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));
}
.
.
}
但是背景颜色没有变化,任何帮助将不胜感激!
如果你想直接做,你应该做这样的事情,否则你应该编辑 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 };
}
}
}