Reputation: 2927
When we define a Style in an XAML resource section & set it's TargetType property to a specific control type then we do not need to set Style property to control of that type. e.g.
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Content" Value="Style set for all buttons"/>
<Setter Property="Background" Value="Gray"/>
</Style>
</Window.Resources>
<Grid>
<Button Height="27" HorizontalAlignment="Left" Margin="119,56,0,0" Name="button1" VerticalAlignment="Top" Width="140" />
<Button Style="{x:Null}" Content="No Style" Height="27" HorizontalAlignment="Left" Margin="212,121,0,0" Name="button2" VerticalAlignment="Top" Width="141" />
<Button Content="Button" Height="25" HorizontalAlignment="Left" Margin="296,183,0,0" Name="button3" VerticalAlignment="Top" Width="158" />
</Grid>
But when we want to define a style for all controls in a window then we need to add Style property to each control to have that defined style. e.g.
<Window.Resources>
<Style x:Key="Style1" TargetType="Control">
<Setter Property="Control.Background" Value="Gray"/>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource Style1}" Content="Button" Height="23" HorizontalAlignment="Left" Margin="67,52,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<Label Style="{StaticResource Style1}" Content="Label" Height="27" HorizontalAlignment="Left" Margin="189,99,0,0" Name="label1" VerticalAlignment="Top" Width="83" />
</Grid>
Can't we access style "Style1" with Button & Label in above example without setting Style property specifically in Button & Label controls?
Thanks in advance.
Upvotes: 0
Views: 308
Reputation: 541
Similar question as to here
General consensus is that you cannot apply a style to something as esoteric as just the control. Considering the wide range of properties and controls, I can understand why the system rejects/ignores it. I would suggest maybe something like this to get you fairly close to what you would want:
<Style x:Key="general" TargetType="{x:Type Control}">
<Setter Property="Control.Background" Value="Green"/>
</Style>
<Style BasedOn="{StaticResource general}" TargetType="{x:Type Button}"/>
<Style BasedOn="{StaticResource general}" TargetType="{x:Type Label}"/>
<Style BasedOn="{StaticResource general}" TargetType="{x:Type CheckBox}"/>
Upvotes: 1
Reputation: 44048
Try <Style TargetType="Button" BasedOn="{StaticResource style1}"/>
for buttons, other for labels and so on...
Upvotes: 1