WPF Table Column Sizes

前端 未结 3 758
遥遥无期
遥遥无期 2020-12-29 06:37

I\'m rendering a Table in a WPF FlowDocument using code-behind. But, I\'ve been unable to find an example that shows how to make the table use only the space needed based on

相关标签:
3条回答
  • 2020-12-29 07:11

    I do not think this is possible... the only hacky workaround is to use the BlockUIContainer and a real grid!

    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));
    
    0 讨论(0)
  • 2020-12-29 07:17

    try TableCell.ColoumnSpan and TableCell.RowSpan

    0 讨论(0)
  • 2020-12-29 07:33

    I know the question was asked 9 years ago, but I found out an alternate way to do this without using a BlockUIContainer which quite frankly is a pain when serializing the FlowDocument or when the user is just editing the document in a RichTextBox.

    Add a PreviewMouseMove and PreviewMouseDown handler to every single table cells. In PreviewMouseMove change the cursor to SizeWE whenever adjacent to the cell border. In PreviewMouseDown capture the mouse using the RichTextBox as the source.

    Add a PreviewMouseMove and PreviewMouseUp handler to the RichTextBox. In PreviewMouseMove resize the table column based on the calculated horizontal delta movement. In PreviewMouseUp release the mouse.

    The tricky part is to figure where the cell borders are because there's no way out of the box to just get the cell position or width. So you have to approximate where they are by doing the sum of the PagePadding, Table Padding and column widths.

    0 讨论(0)
提交回复
热议问题