嗨,我认为这是一个初学者的问题。我搜索了所有相关问题。但所有这些都由 .xaml 回答。但是,我需要的是后台代码。
我有一个 TabControl。我需要设置其项目的背景颜色。
我需要在选择、取消选择和悬停时为项目设置不同的颜色。
非常感谢你的帮助。
我想在这里发布我的代码。但是,目前我拥有的只是 TabControl 的一个实例和一个名为 ActiveTabIndex 的属性。
---------------------------------------编辑1--------- --------------------------------------
我添加了一个事件 SelectionChanged
(this.myTabControl 为 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 上时,我需要它为橙色。 (我不确定我说得够清楚。)
另一个问题是我会让用户设置颜色而不是直接设置为红色。我想我需要一些绑定。对于这个问题,我稍后会google一下。我只想说清楚。
非常感谢大家。真的很有帮助。
<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 ----------------
I am not clear enough I think.
Here is what it is now:
It's working fine if the mouse is over other tabs:
But When the mouse is over the circled area, the background color of the selected item should be red instead of orange:
---------------编辑4 -------------------
感谢大家的回复。
现在,经过与我的用户和其他一些人的长时间讨论后,我们希望动态更改背景颜色。用户想要自己设置颜色。基本上,我需要首先进行一些绑定(如果这是术语的话)。我已经尝试过以下方法。但是,所选选项卡不是红色背景的。我用Snoop查看了一下,原来是本地设置了背景为红色。我在这里有点困惑。我四处询问,有人建议我使用 TemplateBinding,因为它位于 ControlTemplate 下。但是,我尝试创建依赖属性和类似的东西。但我不明白为什么要使用 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);
}
}