我想在选择WPF应用程序时更改我的选项卡的背景图像,这是我目前的代码:
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type TabItem}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border>
<Grid>
<Grid>
<Border x:Name="border" CornerRadius="0,0,0,0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,0,0,0" />
</Grid>
<Border BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="arrowOR.png" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
标签本身的背景我设置了ImageBrush ImageSource =“arrowBU.png”。现在由于某种原因,无论是否选择了标签,背景总是arrowBU.png。
编辑:我还需要在悬停选项卡时设置此arrowOR.png,同样在悬停时光标是手而不是箭头。 我还将我的标签设置为禁用,我希望我的用户使用按钮遵循标准流程。
有人可以帮忙吗?
答案 0 :(得分:1)
问题是Background
(带有arrowBU.png
的图片画笔)的本地属性值正在优先考虑。摆脱这种情况,并在问题中包含的TabItem
默认样式中设置TabItem
的默认背景:
<Style TargetType="{x:Type TabItem}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="arrowBU.png" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<!-- blah blah blah -->
如果您只想在某些标签项上设置该背景,请为基于上述样式定义一种特殊样式。 将背景图片画笔设置器移动到新的派生样式中。然后在要使用它的选项卡项上显式设置此样式。
<Style TargetType="TabItem" x:Key="BUBkgTabItem" BasedOn="{StaticResource {x:Type TabItem}}">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="arrowBU.png" />
</Setter.Value>
</Setter>
</Style>
你可以像这样使用它:
<TabControl>
<!-- Without BU background -->
<TabItem Header="Blah">
<!-- stuff -->
</TabItem>
<!-- With BU background -->
<TabItem Header="Planxty" Style="{StaticResource BUBkgTabItem}">
<!-- stuff -->
</TabItem>