【问题标题】:Change color of tabitem for multiple tabcontrols更改多个 tabcontrols 的 tabitem 的颜色
【发布时间】:2015-07-29 15:40:25
【问题描述】:

我在我的项目中使用了两个选项卡控件。如何更改 tabcontrol 当前处于活动/聚焦的 tabitem 的颜色? 这是 xaml 示例:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TabControl x:Name="First" Grid.Column="0">
        <TabItem Header="Test1" Margin="-2,-2,-2,0.2" Width="44" >
            <Label>Text</Label>
        </TabItem>
        <TabItem Header="Test2" Margin="-2,-2,-2,0.2" Width="44" >
            <Label>Text</Label>
        </TabItem>
    </TabControl>
    <TabControl x:Name="Second" Grid.Column="1">
        <TabItem Header="Test1" Margin="-2,-2,-2,0.2" Width="44" >
            <Label>Text</Label>
        </TabItem>
        <TabItem Header="Test2" Margin="-2,-2,-2,0.2" Width="44" >
            <Label>Text</Label>
        </TabItem>
    </TabControl>
</Grid>

它应该看起来像

【问题讨论】:

  • 我试过了。但我几乎没有 tacontrols,它会改变所有选定的 tabitems 的颜色。
  • 它会这样做,因为它会为该特定类型设置样式,如果您想有选择地应用它,请给样式一个键,然后将其应用到您希望具有该行为的每个选项卡控件。如果您想为每个选项卡控件选择不同的选项卡颜色,那么您可能希望沿着自定义控件的路线走。
  • 感谢您的回答。但是,主要问题是,使选定的 tabitem 仅针对焦点/活动 tabcontrol 突出显示,而不是在未聚焦/非活动 tabcontrol 处突出显示另一个选定项
  • 您也可以使用模板中的触发器来执行此操作,有一些属性可以确定项目是否具有可以绑定的焦点。我建议做更多的研究!也许编辑问题以更清楚地了解实际问题。

标签: c# .net wpf


【解决方案1】:

编辑:将标签更改为文本框以演示所需的行为,并将触发器属性更改为 IsKeyboardFocusWithin。

给你:

<Window x:Class="WpfTestApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Border>
                            <Grid>
                                <Grid>
                                    <Border x:Name="border" 
                                            CornerRadius="3,3,0,0"
                                            Background="WhiteSmoke"/>
                                </Grid>
                                    <ContentPresenter ContentSource="Header"
                                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"
                                     Value="True">
                                <Setter TargetName="border"
                                        Property="Background"
                                        Value="LightGray" />
                            </Trigger>
                            <Trigger Property="IsKeyboardFocusWithin"
                                     Value="True">
                                <Setter TargetName="border"
                                        Property="Background"
                                        Value="Yellow" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <Grid Name="MainGrid" Loaded="MainGrid_Loaded">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TabControl x:Name="First" Grid.Column="0">
            <TabItem Header="Test1" Margin="-2,-2,-2,0.2" Width="44" >
                <TextBox>Replace this text</TextBox>
            </TabItem>
            <TabItem Header="Test2" Margin="-2,-2,-2,0.2" Width="44" >
                <TextBox>Replace this text</TextBox>
            </TabItem>
        </TabControl>
        <TabControl x:Name="Second" Grid.Column="1">
            <TabItem Header="Test1" Margin="-2,-2,-2,0.2" Width="44" >
                <TextBox>Replace this text</TextBox>
            </TabItem>
            <TabItem Header="Test2" Margin="-2,-2,-2,0.2" Width="44" >
                <TextBox>Replace this text</TextBox>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

【讨论】:

  • 感谢您的回答,但这并不是我所需要的。您将“IsFocused”用于 tabItem 控件,但我需要在元素内的 tabcontrol ot 获得焦点后突出显示 tabItem 而没有对元素的任何引用。
  • 如果您执行示例,它的行为与您的请求中的“它应该看起来像”完全一样。你是什​​么意思“我需要在元素内的 tabcontrol ot 在没有任何引用元素的情况下获得焦点之后突出显示 tabItem。”?您能否提供一个更大的 XAML 块,在其中实现了我指出的样式,以便我可以看到它不起作用?
  • 您的代码运行良好。我的意思是,必须突出显示选项卡,例如,如果我单击选项卡控件区域的标签,或者它可能是文本块或其他控件。
  • 我明白了。现在就用编辑后的版本试试吧。使用 IsKeyboardFocusWithin 应该可以解决您的问题,但我无法确定您的 TabItems 的真实内容。
  • 如果以上编辑满足您描述的需求,请将其标记为答案。谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-03-27
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2014-11-08
相关资源
最近更新 更多