This is a migrated thread and some comments may be shown as answers.

Problems setting cell style even in example posted

6 Answers 327 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dave
Top achievements
Rank 1
Dave asked on 21 Mar 2012, 01:26 AM
3/20/2012:

I have a need to programmatically build out a RadRichTextBox.

I've been scrounging the documentation and examples and finally got stuck building out a table.

I'm trying to set the style of a single cell to bold, and it was driving me crazy, then I found the post that had the example "CodeBehindDocument.zip" attached.

I jumped in and started using that code and still had problems, so I thought I'd try to run the example and got the same error there.

on the very first Span Style instantiation, it throws an exception:

sp00.Style = new StyleDefinition();

and the exception is

System.InvalidOperationException:UnsupportedStyle type for Span: Unknown

the 'Unknown' part made me possibly think it was the fact that the constructor is empty, so I tried this:

StyleDefinition sd = new StyleDefinition();
sd.SetPropertyValue(Span.FontWeightProperty, FontWeights.Bold);
 
Span sp00 = new Span("Cell 00");
sp00.Style = new StyleDefinition(sd);

but that gave me the same error on the sp00.Style= line

Any assistance on this would be great.

I'm running 2011 Q3 SP1

Thanks!

-Dave

3/21/2012:

I seem to have gotten a bit farther, but am still having problems.

I'm trying to use code from your WPF sample to see if I can make it work in Silverlight 4:

http://www.telerik.com/help/wpf/radrichtextbox-features-document-elements-tables.html#Creating_a_Table_Programatically_at_runtime

I'm looking at that page because I can't find a similar one for Silverlight.

I can get all the way through the table setup including some styles, but it throws an exception on adding the section to the document.

If I leave out the styling, I can add it.

Here's the code that sets up the section and calls the method to build the table:

Section section0 = new Section();
Table TopTable = BuildTable();
section0.Blocks.Add(TopTable);
RichTextPrint.Document.Sections.Add(section0);

The BuildTable method looks like this:

private Table BuildTable()
{
    Table table = new Table(); // 2 rows, 3 columns
    table.Borders.All = new Telerik.Windows.Documents.Model.Border(BorderStyle.None);
    //table.Style = new StyleDefinition(StyleType.Table);
    //table.Style.SpanProperties.FontFamily = new FontFamily("Tahoma");
    //table.Style.SpanProperties.FontSize = Unit.PointToDip(10);
 
    TableRow row0 = new TableRow();
    TableCell cell00 = new TableCell();
    cell00.PreferredWidth = new TableWidthUnit(150);
    Paragraph p00 = new Paragraph();
    Span s00 = new Span("Caption:");
    p00.Inlines.Add(s00);
    //p00.Style = new StyleDefinition(StyleType.Paragraph);
    //p00.Style.SpanProperties.FontWeight = FontWeights.Bold;
    cell00.Blocks.Add(p00);
    row0.Cells.Add(cell00);
 
    TableCell cell01 = new TableCell();
    Paragraph p01 = new Paragraph();
    Span s01 = new Span("Value");
    p01.Inlines.Add(s01);
    cell01.Blocks.Add(p01);
    row0.Cells.Add(cell01);
    table.Rows.Add(row0);
 
    return table;
}

As shown, it will run fine, but the table font is large and not of my selection, and both cells are normal font weight. If I enable either of the two commented sections in the code above, I get a clean compile, the method returns fine, but the sections.Add in the calling block of code throws an exception:

+        _exception    {System.ArgumentNullException: Value cannot be null.
Parameter name: key
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at System.Collections.Generic.ExtensionMethods.GetValueOrNull[TKey,TValue](IDictionary`2 dict, TKey key)
   at Telerik.Windows.Documents.Model.StyleCollection.EnsureStyleRegistered(StyleDefinition styleDefinition)
   at Telerik.Windows.Documents.Model.RadDocument.EnsureElementStyleInDocument(IElementWithStyle documentElement)
   at Telerik.Windows.Documents.Model.RadDocument.CheckForNotRegisteredStyles(DocumentElement documentElement)

Putting the table up and not being able to format cells isn't useful, and I can't find documentation on this.

If you can point me to the documentation or a sample that runs, I'd certainly be appreciative.

Thanks!

-Dave

6 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 22 Mar 2012, 05:35 PM
Hello Dave,

You have two options when it comes to setting the formatting in the document - using styles or setting local values to the properties of the document elements. The styles generally function as the styles in MS Word e.g. Heading 1, Heading 2, Title, etc, whereas the local values of the text font family and font size is what the combo-boxes set.

You can set the local values like this:

p00.FontSize = Unit.PointToDip(11);
s00.FontFamily = new FontFamily("Tahoma");
s00.FontWeight = FontWeights.Bold;


If you decide to utilize styles, you can read more about their use in this article. Your code throws an exception because you don't set a name to the style and don't register it. In order to be able to apply a style, it must have a name and it must be registered in the document StyleRepository. You can change your code like this:

private void AddTable(RadDocument document)
{
    Table table = new Table(); // 2 rows, 3 columns
    table.Borders = new TableBorders(new Telerik.Windows.Documents.Model.Border(BorderStyle.None));
 
    TableRow row0 = new TableRow();
    TableCell cell00 = new TableCell();
    cell00.PreferredWidth = new TableWidthUnit(150);
 
    StyleDefinition paragraphStyle = new StyleDefinition(StyleType.Paragraph);
    paragraphStyle.SpanProperties.FontFamily = new FontFamily("Tahoma");
    paragraphStyle.SpanProperties.FontSize = Unit.PointToDip(10);
    paragraphStyle.Name = "Tahoma10";
    paragraphStyle.DisplayName = "Tahoma 10";
 
    StyleDefinition boldParagraphStyle = new StyleDefinition(StyleType.Paragraph);
    boldParagraphStyle.BasedOn = paragraphStyle;
    boldParagraphStyle.SpanProperties.FontWeight = FontWeights.Bold;
    boldParagraphStyle.Name = "BoldParagraphStyle";
    boldParagraphStyle.DisplayName = "Bold Paragraph";
 
    document.StyleRepository.Add(paragraphStyle);
    document.StyleRepository.Add(boldParagraphStyle);
 
    Paragraph p00 = new Paragraph();
    Span s00 = new Span("Caption:");
    p00.Inlines.Add(s00);
    p00.Style = boldParagraphStyle;
 
    cell00.Blocks.Add(p00);
    row0.Cells.Add(cell00);
 
    TableCell cell01 = new TableCell();
    Paragraph p01 = new Paragraph();
    p01.Style = paragraphStyle;
    Span s01 = new Span("Value");
    p01.Inlines.Add(s01);
    cell01.Blocks.Add(p01);
    row0.Cells.Add(cell01);
    table.Rows.Add(row0);
 
    Section section = new Section();
    section.Blocks.Add(table);
    document.Sections.Add(section);
}

Note that in the sample above, I have used Paragraph styles and not Table Styles. Table styles have been implemented in 2012 Q1, so if you are using 2011.3.1220, they won't work. The same goes for multiple sections - the support for multiple sections was introduced in 2012 Q1 and in the 2011.3.1220 version, the sections will be merged in one.

If you have other questions, do not hesitate to contact us again.

All the best,
Iva Toteva
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Dave
Top achievements
Rank 1
answered on 22 Mar 2012, 05:54 PM
Thanks Iva...

I had read that about sections but forgotten where the change came in.

Since I am still (by customer choice) on 2011 Q3 SP1, and can only have one section... what does that mean about adding a table at runtime?

The paradigm is to make a new section, add the table, add the section to the document.

If I can't do that, then how do I insert a table in 2011 Q3 SP1?

-Dave
0
Accepted
Iva Toteva
Telerik team
answered on 22 Mar 2012, 07:25 PM
Hi Dave,

The multiple section support is not directly related to the insertion of a table at runtime. I mentioned it in order to warn you that after the document is measured, there will be only one section in the document. For example, that has influence on the headers and footers (the headers and footers of the first section will be shown) and even though you can define headers and footers to the new section you are building in code, they will not be used.
Other than that, the approach you have chosen will do what you would expect of it. If you are viewing the document in the editor at the time the insertion happens, you would only need to add a call to the following method:

RichTextPrint.UpdateEditorLayout();

as the layout of the editor will not be updated by adding a section.

The limitation of the approach you have chosen is that you can only add the table at the end or the beginning of the existing document.

Another way to add a table to an existing document is using the DocumentFragment class and the InsertFragment method of RadRichTextBox. The method inserts a document fragment at the caret position (which can be adjusted using the methods of DocumentPosition).
Here is the code achieving the same task:
private RadDocument CreateDocumentWithTable()
{
    RadDocument document = new RadDocument();
    Section section = new Section();
 
    Table table = new Table(); // 2 rows, 3 columns
    table.Borders = new TableBorders(new Telerik.Windows.Documents.Model.Border(BorderStyle.None));
 
    TableRow row0 = new TableRow();
    TableCell cell00 = new TableCell();
    cell00.PreferredWidth = new TableWidthUnit(150);
 
    StyleDefinition paragraphStyle = new StyleDefinition(StyleType.Paragraph);
    paragraphStyle.SpanProperties.FontFamily = new FontFamily("Tahoma");
    paragraphStyle.SpanProperties.FontSize = Unit.PointToDip(10);
    paragraphStyle.Name = "Tahoma10";
    paragraphStyle.DisplayName = "Tahoma 10";
 
    StyleDefinition boldParagraphStyle = new StyleDefinition(StyleType.Paragraph);
    boldParagraphStyle.BasedOn = paragraphStyle;
    boldParagraphStyle.SpanProperties.FontWeight = FontWeights.Bold;
    boldParagraphStyle.Name = "BoldParagraphStyle";
    boldParagraphStyle.DisplayName = "Bold Paragraph";
 
    document.StyleRepository.Add(paragraphStyle);
    document.StyleRepository.Add(boldParagraphStyle);
 
    Paragraph p00 = new Paragraph();
    Span s00 = new Span("Caption:");
    p00.Inlines.Add(s00);
    p00.Style = boldParagraphStyle;
 
    cell00.Blocks.Add(p00);
    row0.Cells.Add(cell00);
 
    TableCell cell01 = new TableCell();
    Paragraph p01 = new Paragraph();
    p01.Style = paragraphStyle;
    Span s01 = new Span("Value");
    p01.Inlines.Add(s01);
    cell01.Blocks.Add(p01);
    row0.Cells.Add(cell01);
    table.Rows.Add(row0);
 
    section.Blocks.Add(table);
    document.Sections.Add(section);
 
    document.MeasureAndArrangeInDefaultSize();
    return document;
}
 
private void addTable_Click(object sender, RoutedEventArgs e)
{
    ////You can adjust the position of the caret
    //this.radRichTextBox.Document.CaretPosition.MoveToLastPositionInDocument();
    DocumentFragment fragmentToInsert = new DocumentFragment(this.CreateDocumentWithTable());
    this.radRichTextBox.InsertFragment(fragmentToInsert);
}

Kind regards,
Iva Toteva
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Dave
Top achievements
Rank 1
answered on 24 Mar 2012, 03:19 AM
Thanks Iva...

That was all great info, and got me well on my way!

I had a couple RadRichTextBoxes that I wanted to pull into the one I'm building along with the other data so I could print, and was able to pull them in as fragments very nicely thanks to your detail.

When I reworked my content to include tables, the table contents is indented from the left by about 1 character width.... since some of the data is in tables and some not, I simply tacked a " " on the front of the non table data to line it up.

But then I hit a snag when I included the other RichTextBox data. My first thought was to include it in a table, but could not find any way to get a RadRichTextBox.Document into a table cell.

What I really need, I think, is the ability to do a LeftIndent on that fragment I'm inserting, but that's not available on fragment.

I thought possibly I could build a Section to hold a paragraph, and set the LeftIndent on the paragraph, then a span, but once again I am back to not knowing how to get the fragment into the span.

I suppose another way to ask this is how do I avoid having the table content being inset by 1 character?

Here's the table declaration and the first row setup... as you can see, I'm setting TextAlignment left, and Borderstyle.None, so I would expect that to be left-justified unless there's a way to set border.collapse?

Table table = new Table(2,2);
table.Borders = new TableBorders(new Telerik.Windows.Documents.Model.Border(BorderStyle.None));
TableRow[] rows = table.Rows.ToArray();
TableCell[] cells0 = rows[0].Cells.ToArray();
foreach (TableCell cell in cells0)
{
    cell.TextAlignment = Telerik.Windows.Documents.Layout.RadTextAlignment.Left;
    cell.VerticalAlignment = Telerik.Windows.Documents.Layout.RadVerticalAlignment.Center;
    cell.Blocks.Add(new Paragraph());
    (cell.Blocks.First as Paragraph).Style = paragraphStyle;
}


Is there a way around this latest issue? 

Thanks!

-Dave
0
Accepted
Ivailo Karamanolev
Telerik team
answered on 26 Mar 2012, 04:58 PM
Hello Dave,

The Table has two properties for spacing/padding: CellSpacing and CellPadding. You can try setting them both to 0 and see if that removes the indentation you're seeing. If that doesn't help, you can contact us again to see how to resolve your issues.

Kind regards,
Ivailo Karamanolev
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Dave
Top achievements
Rank 1
answered on 26 Mar 2012, 05:38 PM
Arrggg...

If I were doing a table in HTML, I would have automatically done just that...

Thanks for the reply... that did the job.

Maybe I won't have any more questions now :)

Thanks a bunch,

-Dave
Tags
RichTextBox
Asked by
Dave
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Dave
Top achievements
Rank 1
Ivailo Karamanolev
Telerik team
Share this question
or