none
How to set width of a table cell?

    Question

  • How do I set the width of a programmatically created tablecell? And how can I set a border around a table? I am adding a table to a FlowDocument in i RichTextBox at runtime.
    Tuesday, January 26, 2010 9:41 AM

All replies

  • The content of a TableCell determines the size of the TableCell. You cannot set a desired size.  For more information on this see http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/816c0e42-953f-4058-8ddc-664143de009c

    To put a border around the Table, set BorderThickness and BorderBrush properties on the Table.

    Tuesday, January 26, 2010 6:02 PM
  • That's a surprise...How to solve it then? I have seen you can set the width of the TableColumns. I am using the TableCell in question to display a linenumber for a line and I want it be just a few points wide.
    Wednesday, January 27, 2010 9:46 AM
  • set the tablecolumn width to have a cell with lesser width...the table wont allow to have cells in a column with different widths
    <TableColumn Width="212.8" />

    for table border use the following

    <

     

    Table BorderThickness="2" BorderBrush="red">

    Wednesday, January 27, 2010 2:56 PM
    Moderator
  • Can you provide some more details about your scenario?  You might be able to set the Width on the first TableColumn, but I can envision scenarios where this might not work.  You would need a width that would work for line 1 as well as line 1000 and so on.  It there a reason why you are using FlowDocuments and Table?  Could your line number column be put in a Grid instead with the text content in a FlowDocument container in the adjacent Grid Column? 
    Thursday, January 28, 2010 12:28 AM
  • It's ok to have the same width for line 1 as well as line 1000, actually that's what I want. I am building an application for creating template documents containing lines with text and a line number. 

    I have a RichTextBox and when clicking on a button, I dynamically add a new tablerow (corresponding to a new line in the document) to a table in the FlowDocument of the RichTextBox. The user can then fill in the text for the particular line in the rightmost tablecell and a linenumber in the leftmost. The problem is that I want to be able to set the width of the leftmost column/cell to something like 5% of the window's width. As it is now, the two cells cover 50% each.
    Thursday, January 28, 2010 9:25 AM
  • Unfortunately you cannot set a TableColumn Width to a % Width, only hard values.  To do this create a TableColumn with some Width and put it in the TableColumn collection of the Table.  You should then have a Table with the first Column of set Width with a second column that takes up the rest of the space.

    However, you may still want to consider my suggestion of redesiging your container to put the line numbers outside of the RichTextBox.  You should be able to style and format the RichTextBox, FlowDocument, and line number container so that it looks like they are in the same document and line up correctly.  There are several reasons to consider this:

    1. Performance and Scalability.  From what you describe above it sounds like you intend to have a Document with one large Table with multiple TableRows depending on user input.  Tables were not really intended for this and you will probably have scalability/perf problems with large documents.

    2. Selection.  I would think that you would like to retain selection in your documents right?  However, with the single Table solution, selection will look funky because selection regions will include line numbers and that is probably not a desirable outcome.

    3. Editing. Do you want users to be able to edit line numbers?  What if a user wants to delete the content of a Row?  If you were to do this in a single Table you would need some complicated logic to renumber line numbers. 

    Thursday, January 28, 2010 6:14 PM
  • Just for fun I have provided a xaml mock up of what I have proposed:

    <Grid >  
      <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" MinWidth="40"/>
      <ColumnDefinition Width="20*"/>
      </Grid.ColumnDefinitions> 
      <Grid Grid.Column="1">
        <RichTextBox BorderThickness="0" Padding="0,8,0,0">    
          <FlowDocument >
          <FlowDocument.Resources>
             <Style TargetType="Paragraph">
              <Setter Property="FontFamily" Value="Tahoma" />
              <Setter Property="FontSize" Value="14" />
             </Style>
          </FlowDocument.Resources>
            <Paragraph >This is Paragraph 1. A single line Paragraph.</Paragraph>
            <Paragraph>This is Paragraph 2. This Paragraph has multiple lines of content that should wrap depending on the Width of the containing Window. There is enough content in this Paragraph to take up two or more lines.</Paragraph>
            <Paragraph>This is Paragraph 2. This Paragraph has multiple lines of content that should wrap depending on the Width of the containing Window. There is enough content in this Paragraph to take up two or more lines.</Paragraph>
          </FlowDocument>
        </RichTextBox>
      </Grid>
       <Grid>
        <StackPanel>
           <StackPanel.Resources>
            <Style TargetType="TextBlock">
              <Setter Property="FontFamily" Value="Tahoma" />
              <Setter Property="FontSize" Value="14" />         
              <Setter Property="Margin" Value="5,0,8,0" />          
              <Setter Property="TextAlignment" Value="Right" />
              <Setter Property="Foreground" Value="Brown" />
            </Style>
           </StackPanel.Resources>
           <TextBlock Text="1" Margin="5,8,8,0" />
           <TextBlock Text="2" />
           <TextBlock Text="3" />
           <TextBlock Text="4" />
           <TextBlock Text="5" />
           <TextBlock Text="6" />
           <TextBlock Text="7" />
           <TextBlock Text="8" />
           <TextBlock Text="9" />
           <TextBlock Text="10" />
           <TextBlock Text="11" />
           <TextBlock Text="12" />
           <TextBlock Text="13" />
        </StackPanel>
       </Grid>
      </Grid>
    Thursday, January 28, 2010 7:08 PM
  • Thanks, but my application serves to create templates of already drawn up documents for a particular system. That means I get a file (in for example MS Word och Pdf format) with text containing numbered lines which I must reproduce exactly as they appear. For example if the recieved document looks something like this;

    1. PARAGRAPH NUMBER ONE
    2. This is the second line..
    3...and this is the third
    4. PARAGRAPH NUMBER TWO

    ...then, in my application, I am first adding a table to my FlowDocument (representing a paragraph) and then I am adding the lines one by one. The second line must for example be associated with the line number 1 and begin with "This is" and so on. I hope it makes sense.

    Therefore I guess I need to have the lines and their correspondig line numbers in the same container?
    Friday, January 29, 2010 11:15 AM
  • Hmm, I'm not really sure what your solution would be then.  To have the line numbers in the document itself seems like a bizarre layout.  It seems to me that the line numbers should be independent of the content so that if the content changes, the line numbers change to reflect that.  Imagine an edit to the document that adds a new Paragraph.  With independent line numbers it should look like this:

    1. PARAGRAPH NUMBER ONE
    2. This is the second line..
    3. Hey, I added a Paragraph.
    4...and this is the third
    5. PARAGRAPH NUMBER TWO

    If you put the line numbers in the Table, you would need to go through an awful lot of work to renumber everything - checking TableCells and reseting content on any change (yuck).

    As for reproducing the lines exactly as they appear (mentioned in your last reply) is there different implementations of line numbers in different docs?  Could you give an example of a document that would not work with an independent line numbering system outside the RichTextBox?

    Friday, January 29, 2010 6:03 PM
  • In my case there are no documents that will work with independent line numbering system outside the RichTextBox. The line numbers are dependent of the content at all times.

    Monday, February 01, 2010 11:37 AM