How can I add a DateField to a TableCell from code

1 Answer 47 Views
RichTextBox
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Steve asked on 16 Sep 2024, 03:33 PM

Hi,

I need to add a DateField to a TableCell in a RichTextBox so that when a user clicks on the cell in the UI a calendar is displayed. 

I can see from the XAML that this is achieved by using the InsertSdtCommand with a CommandParameter of Date, how can a similar result be achieved from within the code.

Thank you.

 

 

1 Answer, 1 is accepted

Sort by
1
Accepted
Dimitar
Telerik team
answered on 17 Sep 2024, 05:03 AM

Hello Steve,

The following article shows this: WPF RichTextBox - Working with Content Controls - Telerik UI for WPF

Let me know if you have additional questions.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Steve
Top achievements
Rank 2
Iron
Iron
Iron
commented on 17 Sep 2024, 02:08 PM

Hi Dimitar,

Thank you again for pointing me in the right direction. I'm using the following code to add the 'DatePicker' to a table cell:


                RadDocumentEditor editor = new RadDocumentEditor(document);
                var cell = table.EnumerateChildrenOfType<TableCell>().FirstOrDefault();
                var row = table.EnumerateChildrenOfType<TableRow>().LastOrDefault();

                // Insert a field so we can identify the type of template
                if (cell != null)
                {
                    DocumentPosition position = new DocumentPosition(document);
                    position.MoveToDocumentElementStart(cell.Blocks.First());
                    document.CaretPosition.MoveToPosition(position);
                    DocumentVariableField docVariable = new DocumentVariableField() { VariableName = "2000" };
                    editor.InsertField(docVariable);

                }
                // Add a date picker to the the the first column in the second row
                if (row != null) 
                {
                    var dateCell = row.EnumerateChildrenOfType<TableCell>().FirstOrDefault();
                    DocumentPosition position = new DocumentPosition(document);
                    position.MoveToDocumentElementStart(dateCell.Blocks.First());
                    document.CaretPosition.MoveToPosition(position);
                    editor.ChangeFontFamily(new FontFamily(CGlobals.docu_default_font));
                    editor.ChangeFontSize(Unit.PointToDip(CGlobals.docu_default_font_size));
                    DateProperties dateProperties = new DateProperties();
                    dateProperties.FullDate = DateTime.Now;
                    dateProperties.ID = 2001;
                    dateProperties.IsTemporary = false;
                    editor.InsertStructuredDocumentTag(dateProperties);
                }
                doc_text.Document = document;

This works fine, except for the font and font size of the DateProperties content control. It is defaulting to Verdana 12. Is it possible to change the font and font size of the DateProperties content control?

Steve
Top achievements
Rank 2
Iron
Iron
Iron
commented on 17 Sep 2024, 04:21 PM | edited

Okay, finally found it, I needed to create a StyleDefinition and then use that for the StyleName in the DateProperties content control:


                StyleDefinition styleDefinition = doc_text.Document.StyleRepository[RadDocumentDefaultStyles.DefaultTableGridStyleName];
                styleDefinition.Name = "TemplateFields";
                styleDefinition.SpanProperties.FontFamily = new FontFamily(CGlobals.docu_default_font);
                styleDefinition.SpanProperties.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);

                // Add a date picker to the the the first column in the second row
                if (row != null) 
                {
                    var dateCell = row.EnumerateChildrenOfType<TableCell>().FirstOrDefault();
                    DocumentPosition position = new DocumentPosition(document);
                    position.MoveToDocumentElementStart(dateCell.Blocks.First());
                    document.CaretPosition.MoveToPosition(position);
                    editor.ChangeFontFamily(new FontFamily(CGlobals.docu_default_font));
                    editor.ChangeFontSize(Unit.PointToDip(CGlobals.docu_default_font_size));
                    DateProperties dateProperties = new DateProperties();
                    dateProperties.FullDate = DateTime.Now;
                    dateProperties.ID = 2001;
                    dateProperties.IsTemporary = false;
                    dateProperties.StyleName = "TemplateFields";
                    editor.InsertStructuredDocumentTag(dateProperties);
                }

 

Sorry for the bombardment of posts and comments,but we are converting a large WPF app which relies heavily on RichTextBoxes to edit RTF docs. Some of the functionality is to insert predefined templates which have fields for date values, plus much more.

 

Hello Telerik, and goodbye TX Text Control.

Dimitar
Telerik team
commented on 18 Sep 2024, 03:27 PM

Hi Steve,

I am glad that you have found a solution.

Do not hesitate to contact us if you have other questions.
Tags
RichTextBox
Asked by
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or