【问题标题】:WPF FlowDocument - style paragraphs in listitemsWPF FlowDocument - 列表项中的样式段落
【发布时间】:2011-12-02 18:31:57
【问题描述】:

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

使用此 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: How to convert FlowDocument to rtf

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

【问题讨论】:

    标签: wpf margin listitem paragraph


    【解决方案1】:

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

    【讨论】:

      【解决方案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 中的常规段落不同。

      【讨论】:

        猜你喜欢
        • 2010-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多