2009-09-29 46 views
3

我想要一個表根據內容在邏輯上對列進行大小調整。這在WPF中可能嗎?WPF FlowDocument表 - AutoFit選項?

alt text http://img43.imageshack.us/img43/2640/flowdocument.jpg

這是我的工作代碼:

<Window x:Class="FlowDocument.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <Style TargetType="{x:Type TableCell}"> 
      <Setter Property="BorderBrush" Value="Gray" /> 
      <Setter Property="BorderThickness" Value="3" /> 


     </Style> 
     <Style TargetType="{x:Type Paragraph}"> 
      <Setter Property="Padding" Value="2, 2, 2, 2" /> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <FlowDocumentScrollViewer> 
      <FlowDocument> 
       <Table> 
        <Table.Columns> 
         <TableColumn Background="LightBlue" /> 
         <TableColumn Background="Coral" /> 
        </Table.Columns> 
        <TableRowGroup> 
         <TableRow> 
          <TableCell> 
           <Paragraph>This is a long piece of text</Paragraph> 
          </TableCell> 
          <TableCell> 
           <Paragraph>This isn't</Paragraph> 
          </TableCell> 
         </TableRow> 
         <TableRow> 
          <TableCell> 
           <Paragraph>This is a another long piece of text. The column should be wider than the other one!</Paragraph> 
          </TableCell> 
          <TableCell> 
           <Paragraph>Ditto</Paragraph> 
          </TableCell> 
         </TableRow> 
        </TableRowGroup> 
       </Table> 
      </FlowDocument> 
     </FlowDocumentScrollViewer> 
    </Grid> 
</Window> 

回答

2

它不太你在找什麼,但你可以做這樣的事情

<Table.Columns> 
    <TableColumn Background="LightBlue" Width="2*" /> 
    <TableColumn Background="Coral" Width="*" /> 
</Table.Columns> 
+0

我猜字面上正確的答案是一個簡單的「不,你不能」。這些信息更有用,所以我會給它打勾。 – 2011-01-04 10:23:49

1

它通過確定柱的最寬單元的期望寬度是可能的。最寬的單元格可以通過循環遍歷所有行來確定單元格的期望寬度並記住最大值。

在這個例子中,所有的列都進行了優化。 19的值可能來自左右單元格填充加單元格邊框厚度。

void autoresizeColumns(Table table) 
{ 
    TableColumnCollection columns = table.Columns; 
    TableRowCollection rows = table.RowGroups[0].Rows; 
    TableCellCollection cells; 
    TableRow row; 
    TableCell cell; 

    int columnCount = columns.Count; 
    int rowCount = rows.Count; 
    int cellCount = 0; 

    double[] columnWidths = new double[columnCount]; 
    double columnWidth; 

    // loop through all rows 
    for (int r = 0; r < rowCount; r++) 
    { 
     row = rows[r]; 
     cells = row.Cells; 
     cellCount = cells.Count; 

     // loop through all cells in the row  
     for (int c = 0; c < columnCount && c < cellCount; c++) 
     { 
      cell = cells[c]; 
      columnWidth = getDesiredWidth(new TextRange(cell.ContentStart, cell.ContentEnd)) + 19; 

      if (columnWidth > columnWidths[c]) 
      { 
       columnWidths[c] = columnWidth; 
      } 
     } 
    } 

    // set the columns width to the widest cell 
    for (int c = 0; c < columnCount; c++) 
    { 
     columns[c].Width = new GridLength(columnWidths[c]); 
    } 
} 


double getDesiredWidth(TextRange textRange) 
{ 
    return new FormattedText(
     textRange.Text, 
     CultureInfo.CurrentCulture, 
     FlowDirection.LeftToRight, 
     new Typeface(
      textRange.GetPropertyValue(TextElement.FontFamilyProperty) as FontFamily, 
      (FontStyle)textRange.GetPropertyValue(TextElement.FontStyleProperty), 
      (FontWeight)textRange.GetPropertyValue(TextElement.FontWeightProperty), 
      FontStretches.Normal), 
      (double)textRange.GetPropertyValue(TextElement.FontSizeProperty), 
     Brushes.Black, 
     null, 
     TextFormattingMode.Display).Width; 
} 
+0

這可能是解決問題的開始。但是如果所需列寬的總和超過可用寬度會發生什麼? – 2014-05-11 22:21:26

+0

親愛的安德魯,在我的解決方案中,我通過檢查可用寬度來處理它。如果總數超過,我正在使用一個因子,它是可用寬度和所有列的所需總寬度的商。不幸的是,我不知道星星的長度。所以如果可用寬度發生變化,它需要重新計算自己代碼中的所有寬度。 – zznobody 2014-05-13 19:30:56