WPF选项卡控件,使用C#代码更改选项卡项的背景色
嗨,我想这是一个初学者的问题。我搜索了所有相关的问题。但是,所有这些问题的答案都是.xaml。但是,我需要的是反向代码。 我有一个TabControl。我需要设置其项目的背景色。 我需要为选中、取消选中和悬停的项目设置不同的颜色。 非常感谢你的帮助。 我想把我的密码贴在这里。然而,目前我只有一个TabControl实例和一个名为ActiveTabIndex的属性 ---------------------------------------编辑1----------------------------------------------- 我已添加事件选择更改 (this.myTabControl作为System.Windows.Controls.TabControl)SelectionChanged+=TabSet\u SelectionChangedWPF选项卡控件,使用C#代码更改选项卡项的背景色,c#,wpf,C#,Wpf,嗨,我想这是一个初学者的问题。我搜索了所有相关的问题。但是,所有这些问题的答案都是.xaml。但是,我需要的是反向代码。 我有一个TabControl。我需要设置其项目的背景色。 我需要为选中、取消选中和悬停的项目设置不同的颜色。 非常感谢你的帮助。 我想把我的密码贴在这里。然而,目前我只有一个TabControl实例和一个名为ActiveTabIndex的属性 ---------------------------------------编辑1-------------------------
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的新手。所以我仍然对此有疑问(对此我很抱歉,请忍受我)。问题如下:
我想在鼠标位于选项卡项上时将背景颜色更改为橙色。现在,当鼠标位于ContentPanel和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>
你发现自己的问题很难得到答案的原因是因为你完全走错了路;我能想到的只有极少数情况下,按照您的建议更改代码中的控件是合理的。WPF是专门设计用来将可视状态与代码分离的,忽略它会有风险 要真正回答你的问题,以下几点就可以了…有点
foreach (TabItem item in tabControl.Items)
item.Background = new SolidColorBrush(Colors.Blue);
如果您这样做,您会注意到它会更改未选定选项卡的背景色,但不会更改当前选定选项卡的背景色。您还将注意到,用鼠标高亮显示选项卡将再次显示另一种颜色。事实上,TabItem有7种不同的可视状态,添加代码来覆盖每种情况开始变得混乱,当然比使用XAML复杂得多
通过代码控制背景颜色的“正确”方法是使用XAML和数据绑定。首先,转到,在那里可以找到TabItem的完整模板。将它粘贴到Window.Resources部分,现在您可以开始摆弄它的外观了(别忘了添加名称空间和资源)。如果您使用各种资源,您会注意到TabItems使用线性渐变,ControlLightColor用于所有选项卡的顶部,ControlMediumColor用于未选定选项卡的底部,ControlDarkColor用于当前选定选项卡的底部。要在运行时控制这一点,您需要找到样式中分散的这些资源的所有实例,并将它们绑定到代码中的属性,然后编写一个ValueConverter,将任何属性(我猜是布尔值)转换为笔刷。另一种(更好的)方法,也就是根本不需要编写任何额外代码的方法,是使用DataTrigger来更改TabItem的视觉外观,以响应属性更改值,但这已经过了“初学者”阶段,您需要提供有关具体情况的更多信息
更新:这似乎对我有用:
void TabSet_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
foreach (TabItem item in tabControl.Items)
item.Background = new SolidColorBrush(item.IsSelected ? Colors.Green : Colors.Red);
}
我仍然认为这是非常错误的。如果您绝对坚持在代码中这样做,那么就不应该使用WPF。这是完全错误的技术,我怎么强调都不够
更新#2:您就快到了,只需在承载选项卡控件的窗口中执行此操作:
<Window x:Class="MyWpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowState="Maximized">
<Window.Resources>
<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>
</Window.Resources>
<TabControl>
<TabItem Header="Foo" />
<TabItem Header="Bar" />
<TabItem Header="Baz" />
</TabControl>
</Window>
WPF允许您基于现有控件创建新的自定义控件类型,然后您可以在Microsoft站点上用模板/样式声明填写它,并更改位以适合您。创建一个名为MyTabControl的新用户控件,并用以下代码替换隐藏的代码:
public partial class MyTabControl : TabControl
{
public static readonly DependencyProperty SelectedBgClrProperty = DependencyProperty.Register("SelectedBgClr",
typeof(Brush), typeof(MyTabControl), new UIPropertyMetadata(null));
[Category("Appearance")]
public Brush SelectedBgClr
{
get
{
return (Brush)GetValue(SelectedBgClrProperty);
}
set
{
SetValue(SelectedBgClrProperty, value);
}
}
public MyTabControl()
{
InitializeComponent();
}
}
现在用以下内容替换xaml(您需要将名称空间更改为您的项目所需的名称空间):
<Window x:Class="MyWpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowState="Maximized">
<Window.Resources>
<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>
</Window.Resources>
<TabControl>
<TabItem Header="Foo" />
<TabItem Header="Bar" />
<TabItem Header="Baz" />
</TabControl>
</Window>
public partial class MyTabControl : TabControl
{
public static readonly DependencyProperty SelectedBgClrProperty = DependencyProperty.Register("SelectedBgClr",
typeof(Brush), typeof(MyTabControl), new UIPropertyMetadata(null));
[Category("Appearance")]
public Brush SelectedBgClr
{
get
{
return (Brush)GetValue(SelectedBgClrProperty);
}
set
{
SetValue(SelectedBgClrProperty, value);
}
}
public MyTabControl()
{
InitializeComponent();
}
}
<TabControl x:Class="TabDemo.MyTabControl"
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"
Name="tabControl"
d:DesignHeight="300" d:DesignWidth="300">
<TabControl.Resources>
<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 ElementName=tabControl, Path=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>
</TabControl.Resources>
<TabControl.Style>
<Style TargetType="{x:Type TabControl}">
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="SnapsToDevicePixels"
Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid KeyboardNavigation.TabNavigation="Local">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.BorderBrush).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="#FFAAAAAA" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TabPanel x:Name="HeaderPanel"
Grid.Row="0"
Panel.ZIndex="1"
Margin="0,0,4,-1"
IsItemsHost="True"
KeyboardNavigation.TabIndex="1" />
<Border x:Name="Border"
Grid.Row="1"
BorderThickness="1"
CornerRadius="2"
KeyboardNavigation.TabNavigation="Local"
KeyboardNavigation.DirectionalNavigation="Contained"
KeyboardNavigation.TabIndex="2" Background="{Binding ElementName=tabControl, Path=SelectedBgClr}">
<ContentPresenter x:Name="PART_SelectedContentHost"
Margin="4"
ContentSource="SelectedContent" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Style>
<local:MyTabControl SelectedBgClr="Red">
<TabItem Header="Foo" />
<TabItem Header="Bar" />
<TabItem Header="Baz" />
</local:MyTabControl>
foreach (TabItem AllTabItems in MyTabControl.Items) // Change MyTabControl with your TabControl name
{
if (!AllTabItems.IsSelected)
{
// do something for all unselected tabitems
}
else if (AllTabItems.IsSelected)
{
// do something for the selected tabitem
}
else if (AllTabItems.IsMouseOver)
{
// do something for mouseover tabitem
}
}