Reputation: 81
I've a TabControl
with TabControl.Resources
defined at the beginning. This is working nicely but the problem is that I've another TabControl inside the parent TabControl
, and I don't want the style to be applied to the child TabControl
.
Here's an example:
<TabControl>
<TabControl.Resources>
some style and triggers
</TabControl.Resources>
<TabItem>
//Style correctly applied here - there is an external control with a tab item
</TabItem>
</TabControl>
The external control is created by me, I simply split the xaml in other file, this has another TabControl and I don't the style of the parent tab to be applied there.
What's the solution?
Upvotes: 2
Views: 810
Reputation: 5083
The problem is that your style has no
x:Key
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Foreground" Value="Red"></Setter>
</Style>
</TabControl.Resources>
<TabItem Header="Tab 01">
//Style correctly applied
here there is an external control with a tab item
</TabItem>
<TabItem Header="Tab 02">//Style correctly applied
here there is an external control with a tab item
</TabItem>
</TabControl>
</Grid>
To avoid a Style from being applied to all Controls of the same type definded in
TargetType
you need to provide aResouceKey
.To apply the Style with an
x:Key
you now need to specify it in theStyle
property of the control you want to use it on.This is done with:
Style="{StaticResource StyleName}"
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl>
<TabControl.Resources>
<Style x:Key="CustomStyle" TargetType="TabItem">
<Setter Property="Foreground" Value="Red"></Setter>
</Style>
</TabControl.Resources>
<TabItem Header="Tab 01">
//No Style will be applied
</TabItem>
<TabItem Header="Tab 02" Style="{StaticResource CustomStyle}">//Style correctly applied
here there is an external control with a tab item
</TabItem>
</TabControl>
</Grid>
This will only apply the Style
to TabItem
s where you have set the Style.
Upvotes: 2
Reputation: 8803
Like below code:
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem" x:Key="tabItemStyle">
<Setter Property="Foreground" Value="Red" />
<Setter Property="Header" Value="Styled Header" />
</Style>
</TabControl.Resources>
<TabItem Style="{StaticResource tabItemStyle}">
</TabItem>
<TabItem Header="Simple Header">
</TabItem>
</TabControl>
OUTPUT:
Upvotes: 2