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

Codebehind Finding a Span

3 Answers 134 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 13 Apr 2012, 12:01 PM
I load a XAML document into a richtextbox control.
And would like to be able to manipulate some of the information in it at run time.
I have given some of the span tags names like this:
<telerik:TableRow>
                         <telerik:TableCell ColumnSpan="2">
                                <telerik:Paragraph>
                                    <telerik:Span x:Name="uxClientName" Text="Client Name" />
                                </telerik:Paragraph>
                            </telerik:TableCell>
                        </telerik:TableRow>
As you can see inside a tenable row i have a span for some text with a name.
Now i would like to be able to change the text at run time.
How can i find this specific span inside my document?
envisaged something to this effect
Span uxClientName = radRichTextBox.Document.FindSpanByName("");
The FindSpanByName, of course, does not exists as a method of RadDocument. 
How would you go about locating the elements so it can be changed in code behind?

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 17 Apr 2012, 04:56 PM
Hello Andrew,

The name of Span elements is not persisted, as the document structure and the creation, splitting, merging and copying of document elements is controlled by RadDocument. If such properties were to be preserved, the respective behavior in each of these cases must be strictly determined.
 
We don't know your entire scenario but in similar cases, we recommend using Custom Annotation Ranges because you can define different properties that they will preserve, specify the behavior when copy/pasting, etc. For example in our structured content editing demo we select the annotation range with name semanticRangeName using the following code segment:

foreach (RecipeRangeStart rangeStart in document.GetAnnotationMarkersOfType<RecipeRangeStart>())
{
    if (rangeStart.Name == semanticRangeName)
    {
        semanticRangestart = rangeStart;
        semanticRangeEnd = (RecipeRangeEnd)rangeStart.End;
    }
}

 
There is also an article about annotation ranges, which can get you started with them.
Don't hesitate to contact us if you have other questions. Regards,
Martin
the Telerik team

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

0
Andrew
Top achievements
Rank 1
answered on 19 Apr 2012, 03:56 AM
Hi Martin,

I don't really understand how this applies to my problem. I think maybe the example is a little bit too complex for me to be able to get the one thing i need from it.
Do you have a simpler example on how annotations work?

All i am trying to do is something rather simple, i have a XAML document like this:

<telerik:Section PageMargin="10,10,10,10">
                    <telerik:Table LayoutMode="AutoFit" >
                        <telerik:TableRow>
                            <telerik:TableCell ColumnSpan="2">
                                <telerik:Paragraph TextAlignment="Center">
                                    <telerik:Span Text="Organisation - Time Recording Sheet" />
                                </telerik:Paragraph>
                            </telerik:TableCell>
                        </telerik:TableRow>
                        <telerik:TableRow>
                            <telerik:TableCell ColumnSpan="2">
                                <telerik:Paragraph>
                                    <telerik:Span Text="Client Name" />
                                </telerik:Paragraph>
                            </telerik:TableCell>
                        </telerik:TableRow>
                        <telerik:TableRow>
                            <telerik:TableCell>
                                <telerik:Paragraph>
                                    <telerik:Span Text="Cell 1" />
                                </telerik:Paragraph>
                            </telerik:TableCell>
                            <telerik:TableCell>
                                <telerik:Paragraph>
                                    <telerik:Span Text="Cell 2" />
                                </telerik:Paragraph>
                            </telerik:TableCell>
                        </telerik:TableRow>
                        <telerik:TableRow>
                            <telerik:TableCell ColumnSpan="2">
                                <telerik:Paragraph>
                                    <telerik:Span Text="Cell 3" />
                                </telerik:Paragraph>
                            </telerik:TableCell>
                        </telerik:TableRow>                   
                    </telerik:Table>
                </telerik:Section>

And i would like to be ale to change the text where it says "Client Name" dynamically. Just load anything i want at run time.
0
Accepted
Martin Ivanov
Telerik team
answered on 23 Apr 2012, 05:31 PM
Hello Andrew,

The easiest solution to your scenario will be using Mail merge. You can have the following document:
<telerik:RadDocument>
    <telerik:Section PageMargin="10,10,10,10">
        <telerik:Paragraph />
        <telerik:Table LayoutMode="AutoFit" >
            <telerik:TableRow>
                <telerik:TableCell ColumnSpan="2">
                    <telerik:Paragraph TextAlignment="Center">
                        <telerik:Span Text="Organisation - Time Recording Sheet" />
                    </telerik:Paragraph>
                </telerik:TableCell>
            </telerik:TableRow>
            <telerik:TableRow>
                <telerik:TableCell ColumnSpan="2">
                    <telerik:Paragraph>
                        <telerik:Span Text="Client Name" />
                    </telerik:Paragraph>
                </telerik:TableCell>
            </telerik:TableRow>
            <telerik:TableRow>
                <telerik:TableCell>
                    <telerik:Paragraph>
                        <telerik:FieldRangeStart AnnotationID="1">
                            <telerik:MergeField DisplayMode="DisplayName"  PropertyPath="ClientName"/>
                        </telerik:FieldRangeStart>
                        <telerik:Span Text="«ClientName»" />
                        <telerik:FieldRangeEnd AnnotationID="1" />
                    </telerik:Paragraph>
                </telerik:TableCell>
                <telerik:TableCell>
                    <telerik:Paragraph>
                        <telerik:FieldRangeStart AnnotationID="2">
                            <telerik:MergeField DisplayMode="DisplayName" PropertyPath="ClientAge"/>
                        </telerik:FieldRangeStart>
                        <telerik:Span Text="«ClientAge»"/>
                        <telerik:FieldRangeEnd AnnotationID="2" />
                    </telerik:Paragraph>
                </telerik:TableCell>
            </telerik:TableRow>
        </telerik:Table>
        <telerik:Paragraph />
    </telerik:Section>
</telerik:RadDocument>


and set a mail merge data source to the document the following way.

public class MailClientInfo
    {
        public string ClientName { get; set; }
        public int ClientAge { get; set; }
    }
 
 this.radRichTextBox.Document.MailMergeDataSource.ItemsSource = new List<MailClientInfo>()
            {
               new MailClientInfo()
               {
                   ClientAge = 25,
                   ClientName = "Peter",
               },
 
                new MailClientInfo()
               {
                   ClientAge = 30,
                   ClientName = "John",
               }
            };

Then you can change the visible results and iterate trough them using the RRTB public methods or the Ribbon UI mailings from the ribbon.

this.radRichTextBox.Document.ChangeAllFieldsDisplayMode(FieldDisplayMode.Result);
this.radRichTextBox.PreviewNextMailMergeDataRecord();
this.radRichTextBox.PreviewPreviousMailMergeDataRecord();

Here is a link to the complete demo.
If you encounter any problems, contact us again.

All the best,
Martin
the Telerik team

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

Tags
RichTextBox
Asked by
Andrew
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or