2009-07-28 64 views
0

以下是我目前必须在WPF中创建带下划线的标题文本的代码。使用表格必须有一个更简单的方法。如何让底线/边框一直跨越容器

<Grid> 
<FlowDocumentScrollViewer> 
    <FlowDocument> 
    <Table> 
    <Table.Columns> 
     <TableColumn Width="Auto"/> 
    </Table.Columns> 
    <TableRowGroup> 
     <TableRow> 
     <TableCell> 
      <Paragraph Style="{StaticResource Text_LoginHeaderStyle}"> 
      <Bold>Some header text</Bold> 
      </Paragraph> 
     </TableCell > 
     </TableRow> 
     <TableRow> 
     <TableCell> 
      <BlockUIContainer> 
      <Line Style="{StaticResource Control_TitleLineSeparator}" /> 
      </BlockUIContainer> 
     </TableCell> 
     </TableRow> 
    </TableRowGroup> 
    </Table> 
    </FlowDocument> 
</FlowDocumentScrollViewer> 
</Grid> 

直线的定义是

<Style x:Key="Control_TitleLineSeparator" TargetType="Line" BasedOn="{StaticResource BasicHorizontalLine}"> 
    <Setter Property="Stroke" Value="Gray"/> 
    <Setter Property="StrokeThickness" Value="1"/> 
    <Setter Property="Margin" Value="0,0,0,3"/> 
</Style> 

的想法是创造一些文本和具有它强调使得下划线伸展封闭容器的整个宽度,在这种情况下,网格布局。

更新:

我没有使用表,这是我能找到的工作,并没有把文字与线之间的巨大空间的唯一方法。我现在已经找到了看起来更简单的方法。

<BlockUIContainer> 
    <TextBlock Style="{StaticResource Text_LoginHeaderStyle}" Text="Skill Groups"/> 
</BlockUIContainer> 
<BlockUIContainer> 
    <Line Style="{StaticResource Control_TitleLineSeparator}" /> 
</BlockUIContainer> 

但即使这似乎非常复杂。我已经修改了上面的文字以便自成体系。

+0

你能发布这类自成一个例子吗?上面的代码缺少一些资源。另外,你是否必须使用表格? – 2009-07-28 21:29:28

回答

1

你可以尝试包装你想在一个边界,只有具有边框底部中定义的文本:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 

    <Border BorderThickness="0,0,0,1" 
      BorderBrush="Black" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Top" 
      SnapsToDevicePixels="True"> 
    <TextBlock Text="Some text here" /> 
    </Border> 

</Page> 
+0

无论出于何种原因,边界元素都会创建一条更厚,更粗糙的线,然后是Line。 Line创建一个不错的单个像素线。 – 2009-07-28 22:02:57