gpt4 book ai didi

wpf - TabItem.IsSelected 的触发器不起作用

转载 作者:行者123 更新时间:2023-12-04 19:02:16 24 4
gpt4 key购买 nike

我想将选定的选项卡背景设置为红色,而未选择的选项卡背景设置为绿色。但是,只有未选中的选项卡在更改时才会显示为绿色。选择保持白色。

        <TabControl Background="LightGray" Name="MainTabControl">
<TabControl.Resources>
<Style TargetType="TabItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</TabControl.Resources>
<TabItem Header="Main" />
<TabItem Header="Optimizer" />
</TabControl>

enter ifdsfdsre

最佳答案

覆盖 TabItemControlTemplate并向其添加触发器

<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border" >
<ContentPresenter VerticalAlignment="Center" Margin="5"
HorizontalAlignment="Center"
ContentSource="Header" >
</ContentPresenter>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="Red" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="Green" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

编辑

保留原版 tabitem样式并添加背景颜色,从 vs 设计师右键单击 tabitem并选择 EditTemplate > Edit a Copy,然后更新 TabItem.Static.Background Gardian画笔负责未选中的标签颜色,以及 TabItem.Selected.Background负责选定选项卡颜色的画笔:
  //..
<LinearGradientBrush x:Key="TabItem.Static.Background" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#F0F0F0" Offset="0.0"/>
<GradientStop Color="Green" Offset="1.0"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="TabItem.Static.Border" Color="#ACACAC"/>
<LinearGradientBrush x:Key="TabItem.MouseOver.Background" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#ECF4FC" Offset="0.0"/>
<GradientStop Color="#DCECFC" Offset="1.0"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="TabItem.MouseOver.Border" Color="#7EB4EA"/>
<SolidColorBrush x:Key="TabItem.Disabled.Background" Color="#F0F0F0"/>
<SolidColorBrush x:Key="TabItem.Disabled.Border" Color="#D9D9D9"/>
<SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/>
<SolidColorBrush x:Key="TabItem.Selected.Background" Color="Red"/>
//..



或者您可以使用 templateBindingmainBorderinnerBorderBackground属性(最初只有 mainBordertemplateBindedbackground 属性),然后将触发器添加到 controlTemplate触发器,
<Grid x:Name="templateRoot" SnapsToDevicePixels="true">
<Border x:Name="mainBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}" Margin="0">
<Border x:Name="innerBorder" BorderBrush="{StaticResource TabItem.Selected.Border}" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}" Margin="-1" Opacity="0"/>
</Border>
<ContentPresenter x:Name="contentPresenter" ContentSource="Header" Focusable="False" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
</Grid>

并在最后添加您的触发器
              <Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="Green" />
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>

这应该回答你的第二个问题

关于wpf - TabItem.IsSelected 的触发器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com