如何:以编程方式更改内容的 FlowDirection

更新:2007 年 11 月

此示例演示如何以编程方式更改 FlowDocumentReaderFlowDirection 属性。

说明:

这些代码已经过删减。若要查看完整的代码示例,请参见 FlowDirection 属性示例

示例

创建两个 Button 元素,其中每个元素均代表 FlowDirection 的一个可能值。当单击某个按钮时,关联的属性值会应用于名为 tf1 的 FlowDocumentReader 的内容。 该属性值还会写入名为 txt1 的一个 TextBlock

<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,0,0,10">
  <Button Click="LR">LeftToRight</Button>
  <Button Click="RL">RightToLeft</Button>
</StackPanel>

<TextBlock Name="txt1" DockPanel.Dock="Bottom" Margin="0,50,0,0"/>

<FlowDocumentReader>
  <FlowDocument FontFamily="Arial" Name="tf1">
    <Paragraph>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, 
    sed diam nonummy nibh euismod tincidunt ut laoreet dolore 
    magna aliquam erat volutpat.  Ut wisi enim ad minim veniam, 
    quis nostrud exerci tation ullamcorper suscipit lobortis nisl 
    ut aliquip ex ea commodo consequat. Duis autem vel eum iriure.
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed 
    diam nonummy nibh euismod tincidunt ut laoreet dolore magna 
    aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis 
    nostrud exerci tation ullamcorper suscipit lobortis nisl ut 
    uliquip ex ea commodo consequat. Duis autem vel eum iriure. 
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed 
    diam nonummy nibh euismod tincidunt ut laoreet dolore magna 
    aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis 
    nostrud exerci tation ullamcorper suscipit lobortis nisl ut 
    aliquip ex ea commodo consequat. Duis autem vel eum iriure.
    </Paragraph>
    <Paragraph>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed 
    diam nonummy nibh euismod tincidunt ut laoreet dolore magna 
    aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis 
    nostrud exerci tation ullamcorper suscipit lobortis nisl ut 
    aliquip ex ea commodo consequat. Duis autem vel eum iriure. 
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed 
    diam nonummy nibh euismod tincidunt ut laoreet dolore magna 
    aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis 
    nostrud exerci tation ullamcorper suscipit lobortis nisl ut 
    aliquip ex ea commodo consequat. Duis autem vel eum iriure. 
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed 
    diam nonummy nibh euismod tincidunt ut laoreet dolore magna 
    aliquam erat volutpat.  Ut wisi enim ad minim veniam, quis 
    nostrud exerci tation ullamcorper suscipit lobortis nisl ut 
    aliquip ex ea commodo consequat. Duis autem vel eum iriure.
    </Paragraph>
  </FlowDocument>
</FlowDocumentReader>

与上面定义的按钮单击关联的事件在 C# 代码隐藏文件中进行处理。

private void LR(object sender, RoutedEventArgs e)
{
    tf1.FlowDirection = FlowDirection.LeftToRight;
    txt1.Text = "FlowDirection is now " + tf1.FlowDirection;
}
private void RL(object sender, RoutedEventArgs e)
{
    tf1.FlowDirection = FlowDirection.RightToLeft;
    txt1.Text = "FlowDirection is now " + tf1.FlowDirection;
}