1

I am trying to style a FlowDocument to make it behave the same way as when entering text in MS Word. Right now, I am stuck with paragraph margins within a listitem. I have made it look just about the way I want to:

FlowDocument preview

using this XAML:

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <FlowDocument.Resources>

        <Style TargetType="Paragraph">
            <Setter Property="Margin" Value="0,0,0,20" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Margin, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListItem}, AncestorLevel=1}}" Value="0">
                    <Setter Property="Margin" Value="5" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="{x:Type List}">
            <Setter Property="Margin" Value="5" />
        </Style>

    </FlowDocument.Resources>

    <List>
        <ListItem>
            <Paragraph>Bullet1</Paragraph>
        </ListItem>
        <ListItem>
            <Paragraph>Bullet2</Paragraph>
            <List>
                <ListItem>
                    <Paragraph>Bullet2.1</Paragraph>
                    <List>
                        <ListItem>
                            <Paragraph>Punkt 2.1.1</Paragraph>
                        </ListItem>
                    </List>
                </ListItem>
            </List>
        </ListItem>
    </List>

    <Paragraph>Regular paragraph 1</Paragraph>
    <Paragraph>Regular paragraph 2</Paragraph>

</FlowDocument>

My problem now is that I also need to be able to convert the FlowDocument to RTF and make it look good. When converting to RTF, it seems like the the style triggers are ignored.

I use this method to convert to RTF: How to convert FlowDocument to rtf

My question is: Is there any other way to set different margins for a regular paragraph and a paragraph that is child of a listitem? I need to solve this using general styles, and NOT by setting the Style or Margin attribute directly on the paragraphs.

Community
  • 1
  • 1
Christian Myksvoll
  • 634
  • 2
  • 7
  • 16

2 Answers2

2

I found I way to do this myself. Here is the updated FlowDocument.Resources section:

<Style TargetType="Paragraph">
    <Setter Property="Margin" Value="0,0,0,20" />
</Style>

<Style TargetType="ListItem">
    <Style.Resources>            
        <Style TargetType="Paragraph">
            <Setter Property="Margin"  Value="0,0,0,5" />
        </Style>
    </Style.Resources>
</Style>

<Style TargetType="{x:Type List}">
    <Setter Property="Margin" Value="5" />
</Style>

This is exactly what I wanted to do in the first place. Paragraphs inside a ListItem are now styled differently from regular paragraphs in the FlowDocument.

Christian Myksvoll
  • 634
  • 2
  • 7
  • 16
0

FlowDocument is a "live" document. You can interact with it. RTF is not, it is a static presentation file format. When saving the textrange to rtf you basically get a snapshot of the internal state of the control before the triggers take place. That said, you can try defining a new class ListParagraph that derives drom Paragraph and apply the style to your new ListParagraph in the same way you applied to Paragraph. The price for this is that you need to either create ListParagraphs by code (instead of Paragraphs) or replacing Paragraph with ListParagraph in memory right before exporting to RTF.

raf
  • 131
  • 1
  • 4