How would I specify, for example all Paragraph tags inside a Table (excluding those outside the table) in a FlowDocument. I was expecting something like this:
<Style TargetType="Table">
<Style TargetType="Paragraph">
<Setter Property="Margin" Value="0" />
<Style>
</Style>
However nested styles are not allowed like that.
The equivilent to what I want to achieve in CSS would be something like:
Table Paragraph {margin:0}
So all paragraph tags within the scope of the table would have a margin of 0. Is this possible in WPF (in the XAML markup part)? Any good source on complex WPF style selectors would also be appreciated.
I could write code to do it, but that's not what I'm looking for.
It is actually possible to do a kind of style nesting similar to CSS by creating a style and then including one or more styles inside the Style.Resources property like this:
<Style TargetType="Table">
<Style.Resources>
<Style TargetType="Paragraph">
<Setter Property="Padding" Value="0" />
</Style>
</Style.Resources>
</Style>
This style will be applied to all Tables in scope and the nested Paragraph style will be applied to all Paragraphs inside those tables, but not Paragraphs that are outside of Tables. That's because putting things inside the Resources property basically injects them into the visibility scope of all child objects.
You could also technically do multiple levels of nesting like this to color Hyperlinks green that are only inside of Paragraphs, that are only inside of Tables:
<Style TargetType="Table">
<Style.Resources>
<Style TargetType="Paragraph">
<Setter Property="Padding" Value="0" />
<Style.Resources>
<Style TargetType="Hyperlink">
<Setter Property="Foreground" Value="Green" />
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
Unfortunately for you, WPF doesn't work quite like CSS. You can still get the effect that you want, just not quite as simply. You'd need to name the Style and apply it whichever Paragraph elements that you wanted to affect manually:
<Style x:Key="NoSPaceParagraph" TargetType="Paragraph">
<Setter Property="Margin" Value="0" />
</Style>
...
<Paragraph Style="{StaticResource NoSPaceParagraph}">
....
</Paragraph>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With