2009-09-29 4 views
3

내용에 따라 논리적으로 열의 크기를 조정할 테이블이 필요합니다. 이 WPF 가능합니까? 여기 WPF FlowDocument 테이블 - 자동 맞춤 옵션?

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

나는 문자 그대로 정답은 단순한 "아니오, 할 수 없다"고 생각합니다. 이 정보가 더 유용하므로 틱할 것입니다. –

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

그게 문제를 해결하는 시작일 수 있습니다. 그러나 원하는 열 너비의 합계가 사용 가능한 너비를 초과하면 어떻게됩니까? –

+0

친애하는 앤드류, 내 솔루션 난 가능한 너비를 확인하여 처리 할 수 ​​있습니다. 합계가 초과하면, 모든 열의 사용 가능한 너비와 원하는 너비의 몫 인 인수로 작업하고 있습니다. 불행히도 나는 별의 길이에 성공하지 못했습니다. 따라서 사용 가능한 너비가 변경되면 자체 코드에서 모든 너비를 다시 계산해야합니다. – zznobody