1

我正在尝试设置 FlowDocument 的样式,以使其行为方式与在 MS Word 中输入文本时相同。现在,我被一个列表项中的段落边距困住了。我让它看起来就像我想要的那样:

FlowDocument 预览

使用此 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>

我现在的问题是我还需要能够将 FlowDocument 转换为 RTF 并使其看起来不错。转换为 RTF 时,样式触发器似乎被忽略了。

我使用此方法转换为 RTF: 如何将 FlowDocument 转换为 rtf

我的问题是:有没有其他方法可以为常规段落和列表项的子段落设置不同的边距?我需要使用一般样式来解决这个问题,而不是直接在段落上设置 Style 或 Margin 属性。

4

2 回答 2

2

我找到了自己做这件事的方法。这是更新的 FlowDocument.Resources 部分:

<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>

这正是我最初想做的事情。ListItem 中的段落现在的样式与 FlowDocument 中的常规段落不同。

于 2011-10-12T14:01:12.760 回答
0

FlowDocument 是一个“实时”文档。你可以和它互动。RTF 不是,它是一种静态演示文件格式。将 textrange 保存到 rtf 时,您基本上会在触发器发生之前获得控件内部状态的快照。也就是说,您可以尝试定义一个派生 drom Paragraph 的新类 ListParagraph,并以与应用于 Paragraph 相同的方式将样式应用于新 ListParagraph。这样做的代价是您需要在导出到 RTF 之前通过代码(而不是段落)创建 ListParagraphs 或在内存中用 ListParagraph 替换 Paragraph。

于 2011-10-11T07:53:10.487 回答