代码之家  ›  专栏  ›  技术社区  ›  Walt Stoneburner

WPF表格列大小

  •  16
  • Walt Stoneburner  · 技术社区  · 15 年前

    我正在渲染 Table 在WPF中 FlowDocument 使用代码隐藏。但是,我找不到一个示例来说明如何使表只使用所需的空间 based on content . 相反,表格占用了所有可用的宽度,我不想这样做,也不想指定一个精确的像素大小。

    我显然错过了一些简单的东西,有人看到了吗?

    var fd = new FlowDocument();
    
    Table t = new Table();
    
    t.BorderBrush = Brushes.Black;
    t.BorderThickness = new Thickness(2);
    
    // I thought this would do what I wanted...
    t.Columns.Add(new TableColumn() { Width = GridLength.Auto });
    t.Columns.Add(new TableCOlumn() { Width = GridLength.Auto });
    
    TableRowGroup trg = new TableRowGroup();
    
    TableRow currentRow = new TableRow();
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("ABC"))));
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("XYZ"))));
    trg.Rows.Add(currentRow);
    
    currentRow = new TableRow();
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("123"))));
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("789"))));
    trg.Rows.Add(currentRow);
    
    t.RowGroups.Add(trg);
    
    fd.Blocks.Add(t);
    
    2 回复  |  直到 14 年前
        1
  •  9
  •   rudigrobler    15 年前

    var fd = new FlowDocument();
    
    Grid g = new Grid();
    
    g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
    g.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
    
    g.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
    g.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
    
    var t1 = new TextBlock() { Text = "ABC", Margin = new Thickness(5,3,5,3) };
    t1.SetValue(Grid.ColumnProperty, 0);
    t1.SetValue(Grid.RowProperty, 0);
    g.Children.Add(t1);
    
    var t2 = new TextBlock() { Text = "XYZ", Margin = new Thickness(5, 3, 5, 3) };
    t2.SetValue(Grid.ColumnProperty, 1);
    t2.SetValue(Grid.RowProperty, 0);
    g.Children.Add(t2);
    
    var t3 = new TextBlock() { Text = "123", Margin = new Thickness(5, 3, 5, 3) };
    t3.SetValue(Grid.ColumnProperty, 0);
    t3.SetValue(Grid.RowProperty, 1);
    g.Children.Add(t3);
    
    var t4 = new TextBlock() { Text = "789", Margin = new Thickness(5, 3, 5, 3) };
    t4.SetValue(Grid.ColumnProperty, 1);
    t4.SetValue(Grid.RowProperty, 1);
    g.Children.Add(t4);
    
    fd.Blocks.Add(new BlockUIContainer(g));
    
        2
  •  3
  •   icaptan    14 年前