1
我有一个TabControl,并在其他选项卡中我有一个名为“错误”。当某个名为“ErrorsExist”的属性设置为true时,我需要将其头部前景变为红色。这里是我的代码:如何设置TabItem的标题前景?
<TabControl >
<TabControl.Resources>
<conv:ErrorsExistToForegroundColorConverter x:Key="ErrorsExistToForegroundColorConverter"/>
<Style TargetType="{x:Type TabItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Foreground="{Binding Path=ErrorsExist, Converter={StaticResource ErrorsExistToForegroundColorConverter}}" Text="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem x:Name="ErrorsTab" Header="Errors">
这里是我的转换器:
public class ErrorsExistToForegroundColorConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
switch ((bool)value)
{
case true:
return Brushes.Red;
case false:
return Brushes.Black;
default:
return Binding.DoNothing;
}
}
我有两个问题。
首先,这会将所有标签页眉设置为红色,并且我只需要为ErrorsTab选项卡执行此操作。
二,它只是不起作用。我的意思是,转换器的Convert()方法永远不会被调用。你能帮我解决这个问题吗?
谢谢。
非常感谢你,它的工作。我只是想知道为什么它不适用于转换器... – 2012-04-04 13:56:18
不客气。已编辑帖子,并添加了一个基于转换器的解决方案... – SvenG 2012-04-04 15:11:42
奇怪的是,我没有看到它... – 2012-04-04 15:14:47