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

Custom Field

2 Answers 201 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Joël
Top achievements
Rank 2
Joël asked on 27 Sep 2011, 04:10 PM
Is it possible to create personalized fields derived from CodeBasedField ?

And if so, how ? I tested but I am apparently blocked with the GetResultFragment() function.

2 Answers, 1 is accepted

Sort by
0
Accepted
Boby
Telerik team
answered on 30 Sep 2011, 03:02 PM
Hi Laurent,
Custom page fields are not fully supported for now, as there are known issues with their serialization to XAML. If you decide  to use them, you can use the base CodeBasedField.CreateFragmentFromText method inside the GetResultFragment:
public class PagesNumberField : CodeBasedField
{
    public static readonly string FieldType = "NUMPAGES";
 
    static PagesNumberField()
    {
        CodeBasedFieldFactory.RegisterFieldType(PagesNumberField.FieldType, () => { return new PagesNumberField(); });
    }
 
    public override string FieldTypeName
    {
        get { return PagesNumberField.FieldType; }
    }
 
    protected override DocumentFragment GetResultFragment()
    {
        int pagesNumber = 0;
        if (this.Document != null)
        {
            pagesNumber = DocumentStructureCollection.GetChildrenCount(this.Document.DocumentLayoutBox);
        }
 
        string resultString = pagesNumber.ToString();
 
        return this.CreateFragmentFromText(resultString);
    }
 
    public override Field CreateInstance()
    {
        return new PagesNumberField();
    }
}
Note that this is just a sample implementation. It, for example, won't work in headers and footers.

All the best,
Boby
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Joël
Top achievements
Rank 2
answered on 03 Oct 2011, 07:58 AM
It's perfect, thanks a lot.
Tags
RichTextBox
Asked by
Joël
Top achievements
Rank 2
Answers by
Boby
Telerik team
Joël
Top achievements
Rank 2
Share this question
or