I want to apply Padding="0"
to each Label
on the left sidebar (the first StackPanel
) so that it left-aligns with each TextBox
(and other controls).
How can I define an implicit style in <ApplicationResources>
which only applies to elements within a specific container?
Alternatives:
- Use
x:Key="sidebarLabel"
. However, this option seems redundant for the many labels in the sidebar of my actual application. - Add
Padding=0
to eachLabel
in the sidebar. This is essentially the same as the previous alternative. - Move the implicit style into
<StackPanel.Resources>
. However, I want to keep the styles (inApp.xaml
) separate from the XAML (inMainWindow.xaml
).
<Application.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Padding" Value="0" />
</Style>
</Application.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<GroupBox Header="New User">
<StackPanel>
<Label>First Name:</Label>
<TextBox/>
<Label>Last Name:</Label>
<TextBox/>
</StackPanel>
</GroupBox>
</StackPanel>
<GroupBox Grid.Column="1" Header="Main">
<StackPanel>
<Label>I want default padding here</Label>
</StackPanel>
</GroupBox>
</Grid>