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.
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?
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.
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.
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>
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?
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?