如何在 FlowDocument 中水平居中表格或列表?
下面是来自默认 WPF 应用程序的 MainWindow.xaml 的示例代码。文档中的第一个和最后一个“块”是段落,中间的“块”是表格。表格始终显示在其列的左边缘,表格右侧的区域为空白。我想将桌子放在这个空间的中心。
我尝试过将表格放入“部分”、“段落”等中,但没有任何运气。段落内的浮动框可以工作,但随后表格会流入下一个段落。也许我可以通过在左侧和右侧使用两个空列并通过某种方式动态计算它们的宽度来做到这一点,但这看起来有点矫枉过正。
谢谢 吉瓦卡
<Window x:Class="FlowDocument.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="700" Width="700">
<FlowDocumentPageViewer>
<FlowDocument>
<Paragraph FontSize="24">Moons of Saturn</Paragraph>
<Paragraph>The moons of Saturn are numerous and diverse, ranging from tiny moonlets less than 1 kilometre across, to the enormous Titan, which is larger than the planet Mercury. Saturn has 62 moons with confirmed orbits, fifty-three of which have names, and only thirteen of which have diameters larger than 50 kilometres. Saturn has seven moons that are large enough to become spherical, and dense rings with complex orbital motions of their own. Particularly notable among Saturn's moons are Titan, the second largest moon in the Solar System, with a nitrogen-rich Earth-like atmosphere and a landscape including hydrocarbon lakes and dry river networks, and Enceladus, which emits jets of gas and dust and may harbor liquid water under its south pole region.</Paragraph>
<Table TextAlignment="Right" BorderBrush="Black" BorderThickness="1" Background="Black">
<Table.Columns>
<TableColumn Width="100" ></TableColumn>
<TableColumn Width="100"></TableColumn>
</Table.Columns>
<TableRowGroup>
<TableRow>
<TableCell Background="White" Padding="5">
<Paragraph>Name</Paragraph>
</TableCell>
<TableCell Background="White" Padding="5">
<Paragraph>Diameter</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell Background="White" Padding="5">
<Paragraph>Mimas</Paragraph>
</TableCell>
<TableCell Background="White" Padding="5">
<Paragraph>396</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell Background="White" Padding="5">
<Paragraph>Enceladus</Paragraph>
</TableCell>
<TableCell Background="White" Padding="5">
<Paragraph>504</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell Background="White" Padding="5">
<Paragraph>Tethys</Paragraph>
</TableCell>
<TableCell Background="White" Padding="5">
<Paragraph>1062</Paragraph>
</TableCell>
</TableRow>
</TableRowGroup>
</Table>
<Paragraph>Twenty-four of Saturn's moons are regular satellites; they have prograde orbits not greatly inclined to Saturn's equatorial plane. They include the seven major satellites, four small moons which exist in a Trojan orbit with larger moons, two mutually co-orbital moons and two moons which act as shepherds of Saturn's F Ring. Two other known regular satellites orbit within gaps in Saturn's rings. The relatively large Hyperion is locked in a resonance with Titan. The remaining regular moons orbit near the outer edge of the A Ring, within G Ring and between the major moons Mimas and Enceladus. The regular satellites are traditionally named after Titans and Titanesses or other figures associated with the mythological Saturn.</Paragraph>
</FlowDocument>
</FlowDocumentPageViewer>
</Window>
Below is sample code from a default WPF application's MainWindow.xaml. First and last 'Block's in the document are paragraphs and the middle one is a table. Table always appear at the left edge of its column and the area to the right of the table is blank. I would like to center the table in this space.
I have tried putting the table inside a 'Section', a 'Paragraph' etc, but didn't have any luck. A Floater inside a paragraph works, but then the table flow into the next paragraph. Probably I can do this by using two empty columns on the left and right with some way to calculate their widths dynamically, but that looks overkill.
Thanks
Jeevaka
<Window x:Class="FlowDocument.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="700" Width="700">
<FlowDocumentPageViewer>
<FlowDocument>
<Paragraph FontSize="24">Moons of Saturn</Paragraph>
<Paragraph>The moons of Saturn are numerous and diverse, ranging from tiny moonlets less than 1 kilometre across, to the enormous Titan, which is larger than the planet Mercury. Saturn has 62 moons with confirmed orbits, fifty-three of which have names, and only thirteen of which have diameters larger than 50 kilometres. Saturn has seven moons that are large enough to become spherical, and dense rings with complex orbital motions of their own. Particularly notable among Saturn's moons are Titan, the second largest moon in the Solar System, with a nitrogen-rich Earth-like atmosphere and a landscape including hydrocarbon lakes and dry river networks, and Enceladus, which emits jets of gas and dust and may harbor liquid water under its south pole region.</Paragraph>
<Table TextAlignment="Right" BorderBrush="Black" BorderThickness="1" Background="Black">
<Table.Columns>
<TableColumn Width="100" ></TableColumn>
<TableColumn Width="100"></TableColumn>
</Table.Columns>
<TableRowGroup>
<TableRow>
<TableCell Background="White" Padding="5">
<Paragraph>Name</Paragraph>
</TableCell>
<TableCell Background="White" Padding="5">
<Paragraph>Diameter</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell Background="White" Padding="5">
<Paragraph>Mimas</Paragraph>
</TableCell>
<TableCell Background="White" Padding="5">
<Paragraph>396</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell Background="White" Padding="5">
<Paragraph>Enceladus</Paragraph>
</TableCell>
<TableCell Background="White" Padding="5">
<Paragraph>504</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell Background="White" Padding="5">
<Paragraph>Tethys</Paragraph>
</TableCell>
<TableCell Background="White" Padding="5">
<Paragraph>1062</Paragraph>
</TableCell>
</TableRow>
</TableRowGroup>
</Table>
<Paragraph>Twenty-four of Saturn's moons are regular satellites; they have prograde orbits not greatly inclined to Saturn's equatorial plane. They include the seven major satellites, four small moons which exist in a Trojan orbit with larger moons, two mutually co-orbital moons and two moons which act as shepherds of Saturn's F Ring. Two other known regular satellites orbit within gaps in Saturn's rings. The relatively large Hyperion is locked in a resonance with Titan. The remaining regular moons orbit near the outer edge of the A Ring, within G Ring and between the major moons Mimas and Enceladus. The regular satellites are traditionally named after Titans and Titanesses or other figures associated with the mythological Saturn.</Paragraph>
</FlowDocument>
</FlowDocumentPageViewer>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个名为
HorizontalAlignment
的 WPF 元素属性,您希望将感兴趣的元素包围在容器内(例如网格或边框),并将该属性设置为Center
WPF 会执行其余操作魔法。There is a WPF element attribute called
HorizontalAlignment
you would want to surround the element of interest within a container Like a grid or border and set the attribute toCenter
WPF does the rest of the magic.