---------------------------------------编辑1-----------------------------------------------
我添加了一个SelectionChanged事件。
(this.myTabControl as System.Windows.Controls.TabControl).SelectionChanged += TabSet_SelectionChanged;
void TabSet_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
foreach (System.Windows.Controls.TabItem item in (this.myTabControl as System.Windows.Controls.TabControl).Items)
{
if (item == (this.myTabControl as System.Windows.Controls.TabControl).SelectedItem)
{
item.Background = System.Windows.Media.Brushes.Red;
}
else
item.Background = System.Windows.Media.Brushes.Green;
}
}
然而,我只能将它设置为绿色。所选项目的背景颜色仍保持默认颜色而不是红色。对此有什么提示吗? 另外,我想知道如何为悬停添加事件。还没有找到确切的事件。再次感谢。
-----------------------编辑2------------------------------
经过长时间的讨论,我决定(实际上不是我的决定)使用XAML。是的,我是WPF的新手。因此,我仍然有关于此的问题(非常抱歉,希望您能理解)。以下是我的问题: 当鼠标悬停在TabItem上时,我想将背景颜色更改为橙色。现在当鼠标悬停在ContentPanel和TabItem上时,颜色为橙色。我需要它仅在鼠标悬停在TabItem上时为橙色。(我不确定我表达清楚了。) 另一个问题是,我想让用户设置颜色而不是直接设置为红色。我想这需要一些绑定。对于这个问题,我之后会去搜索。只是想弄清楚。 非常感谢大家。真的很有帮助。
<TabItem x:Class="MyControls.Tab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TabItem.Style>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border" Margin="0,0,-4,0" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" >
<ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="100" />
<Setter TargetName="Border" Property="Background" Value="Red" />
<Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="Green" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="Orange" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabItem.Style>
</TabItem>
------------- 编辑3 ----------------
我的表述可能不够清楚。
现在的情况是这样的:
如果鼠标移到其他选项卡上,一切都很正常:
但是当鼠标移到圆形区域时,所选项目的背景颜色应该从橙色变为红色:
---------------编辑4 -------------------
感谢大家的回答。 经过与用户和其他人的长时间讨论,我们希望能够动态更改背景颜色。用户想要自行设置颜色。基本上,我需要先进行一些绑定(如果这是正确的术语)。我尝试了以下操作。然而,所选选项卡没有红色背景。我使用Snoop进行了检查,结果发现背景颜色在本地被设置为红色。我有点困惑了。我向周围的人提出了问题,并得到了建议,说既然它在ControlTemplate下,所以应该使用TemplateBinding。但是,我尝试创建依赖属性等等。但是我不明白为什么要使用TemplateBinding,因为我读了一些文章说它是用于编译时绑定的。我完全困惑了。我是WPF的新手,如果问题太低级,请见谅。 再次感谢!
<TabItem x:Class="MyControl.Tab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TabItem.Style>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border" Margin="0,0,-4,0" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" >
<ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="100" />
<Setter TargetName="Border" Property="Background" Value="{Binding SelectedBgClr}" />
<Setter Property="Foreground" Value="Yellow" />
<Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="Green" />
<Setter Property="Foreground" Value="AliceBlue"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="Orange" />
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabItem.Style>
</TabItem>
后面的代码是:
public Tab()
{
SelectedBgClr = new SolidColorBrush(Colors.Red);
//UnSelectedBgClr = new SolidColorBrush(Colors.Green);
//HoverBgClr = new SolidColorBrush(Colors.Orange);
InitializeComponent();
}
public static readonly DependencyProperty SelectedBgClrProperty = DependencyProperty.Register("SelectedBgClr", typeof(Brush), typeof(Tab), new UIPropertyMetadata(null));
public Brush SelectedBgClr
{
get
{
return (Brush)GetValue(SelectedBgClrProperty);
}
set
{
SetValue(SelectedBgClrProperty, value);
}
}