2011-10-07 72 views
1

我試圖設計一個FlowDocument,使其表現方式與在MS Word中輸入文本時的行爲相同。目前,我在列表項目中留有段落邊距。我已經使它看起來只是我想要的方式:使用此XAMLWPF FlowDocument - 列表項中的樣式段落

FlowDocument preview

<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

我的問題是: 是否有任何其他方式用於常規的段落和段落是一個ListItem的孩子設置不同的利潤率?我需要使用一般樣式來解決這個問題,而不是直接在段落上設置Style或Margin屬性。

回答

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中的常規段落不同。

0

FlowDocument是一個「實時」文檔。你可以與它進行交互。 RTF不是,它是一種靜態演示文件格式。 將textrange保存爲rtf時,基本上會在觸發器發生前獲取控件內部狀態的快照。這就是說,你可以嘗試定義一個新的類ListParagraph,該類派生自Paragraph,並將該樣式應用於新的ListParagraph,方法與應用於Paragraph的方式相同。 這樣做的代價是,您需要在導出到RTF之前通過代碼(而不是段落)創建ListParagraphs或在內存中用ListParagraph替換段落。