Unfortunately, this is not possible in WPF. Closest you can come to this - this is what you demonstrated in your example.
However, if you want to apply the style to all the StackPanels in the root container the same , you can specify the style as the resource of the root container, leaving the x:Key
attribute. As an example:
<Grid x:Name="LayoutRoot"> <Grid x:Name="StackPanelsRoot"> <Grid.Resources> <Style TargetType="StackPanel"> ... </Style> </Grid.Resources> <StackPanel x:Name="SP1" ... /> <StackPanel x:Name="SP2" ... /> ... </Grid> <StackPanel x:Name="SP3" ... /> ... </Grid>
Here the style will be applied to SP1
and SP2
, but not to SP3
source share