wpf create style for named element

Viewed 5762

Is it possible to add style to xaml element without editing the element?

for example:

xaml element:

<Grid>
     <Grid x:Name="A">content A</Grid>
     <Grid x:Name="B">content B</Grid>
</Grid>

and style:

<Style x:Key="StyleForA" TargetName="A" TargetType="{x:Type Grid}" >
   <Setter Property="Background" Value="Red"/>
</Style>
<Style x:Key="StyleForB" TargetName="B" TargetType="{x:Type Grid}" >
   <Setter Property="Background" Value="Green"/>
</Style>

UPD: I have a project with a lot of styles (aero, black, etc ).

And if I edit the element Style="{StaticResources StyleForA}" I must edit all styles. So I need to create local style that affected to named element.

4 Answers
Related