Good evening,
How can I insert text into my RadRichTextBox / Document wrapped with custom annotations?
I have the following code, but it only works the first time the code is run. If I run the code a second time by inserted a second piece of text then the annotations aren't wrapped properly.
Please see my code & XAML output below.
Code to insert text and annotation:
Here is the XAML output when the code is run for the first time (this is correct as it should be):
If I run the code a second time to insert a second piece of text then I get the following output (this isn't what I want):
This is the XAML output that I want to achieve (each piece of inserted text has it's on annotation range):
I think the problem is that I'm not getting the correct start & end positions of the newly inserted text. Please see my c# code "positionStart" & "positionEnd".
Basically my application is an intellisense autocomplete application where the ListBox autcomplete items are drawn from a database. Thus I need the custom annotations to wrap around my text to relate it to the database ID tags. I haven't setup the proper annotation values yet, I'm just playing with the example code from a telerik demo project.
Any advice would be greatly appreciated.
I think I need to achieve the following:
************ EDIT *************
I have tried setting the start and end positions like so but it still doesn't work:
C# Code to insert text & annotations:
XAML Output:
How can I insert text into my RadRichTextBox / Document wrapped with custom annotations?
I have the following code, but it only works the first time the code is run. If I run the code a second time by inserted a second piece of text then the annotations aren't wrapped properly.
Please see my code & XAML output below.
Code to insert text and annotation:
//Focus RichTextBox - I have made my own Intellisense ListBox
radRichTextBox.Focus();
//Get Caret position (start position)
DocumentPosition startPosition =
new
DocumentPosition(radRichTextBox.Document);
//Insert text into document at caret position - text is from Intellisense ListBox selected item
radRichTextBox.Document.Insert(((Products)radListBox.SelectedItem).Name +
": "
, radRichTextBox.Document.Style);
//Get caret position now text has been inserted (end position)
DocumentPosition endPosition =
new
DocumentPosition(
this
.radRichTextBox.Document);
//Adjust position of Intellisense ListBox to new Caret position
AdjustAutoCompletePosition();
//Reload Intellisense ListBox with new items based on previous selection
SetAutoCompleteListBoxItems(((Products)radListBox.SelectedItem).ID);
//Set Custom Annotation
SemanticRangeEnd rangeEnd =
new
SemanticRangeEnd();
SemanticRangeStart rangeStart = (SemanticRangeStart)rangeEnd.CreatePairedStart();
rangeStart.Name =
"SemanticRange "
+ count++;
//Place annotation around newly inserted text - note start & end position
radRichTextBox.Document.InsertCustomAnnotationRange(startPosition, endPosition, rangeStart, rangeEnd);
Here is the XAML output when the code is run for the first time (this is correct as it should be):
<
t:Paragraph
>
<
custom1:SemanticRangeStart
AnnotationID
=
"1"
Name
=
"SemanticRange 0"
/>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Appliances: "
/>
<
custom1:SemanticRangeEnd
AnnotationID
=
"1"
/>
</
t:Paragraph
>
If I run the code a second time to insert a second piece of text then I get the following output (this isn't what I want):
<
t:Paragraph
>
<
custom1:SemanticRangeStart
AnnotationID
=
"1"
Name
=
"SemanticRange 1"
/>
<
custom1:SemanticRangeEnd
AnnotationID
=
"1"
/>
<
custom1:SemanticRangeStart
AnnotationID
=
"2"
Name
=
"SemanticRange 0"
/>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Appliances: "
/>
<
custom1:SemanticRangeEnd
AnnotationID
=
"2"
/>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Hob: "
/>
</
t:Paragraph
>
This is the XAML output that I want to achieve (each piece of inserted text has it's on annotation range):
<
t:Paragraph
>
<
custom1:SemanticRangeStart
AnnotationID
=
"2"
Name
=
"SemanticRange 0"
/>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Appliances: "
/>
<
custom1:SemanticRangeEnd
AnnotationID
=
"2"
/>
<
custom1:SemanticRangeStart
AnnotationID
=
"1"
Name
=
"SemanticRange 1"
/>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Hob: "
/>
<
custom1:SemanticRangeEnd
AnnotationID
=
"1"
/>
</
t:Paragraph
>
I think the problem is that I'm not getting the correct start & end positions of the newly inserted text. Please see my c# code "positionStart" & "positionEnd".
Basically my application is an intellisense autocomplete application where the ListBox autcomplete items are drawn from a database. Thus I need the custom annotations to wrap around my text to relate it to the database ID tags. I haven't setup the proper annotation values yet, I'm just playing with the example code from a telerik demo project.
Any advice would be greatly appreciated.
I think I need to achieve the following:
- Get Caret Position (start position)
- Insert text into document
- Get Carent Position (end position)
- Apply annotation based on start & end positions
Thank you for your time,
Rob
************ EDIT *************
I have tried setting the start and end positions like so but it still doesn't work:
C# Code to insert text & annotations:
//Get caret position (start)
DocumentPosition startPosition =
this
.radRichTextBox.Document.CaretPosition;
//Insert text into document at caret position - text is from Intellisense ListBox selected item
radRichTextBox.Document.Insert(((Products)radListBox.SelectedItem).Name +
": "
, radRichTextBox.Document.Style);
//Get caret position now text has been inserted (end of text)
DocumentPosition endPosition =
this
.radRichTextBox.Document.CaretPosition;
XAML Output:
<
t:Paragraph
>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Appliances: "
/>
<
custom1:SemanticRangeStart
AnnotationID
=
"1"
Name
=
"SemanticRange 0"
/>
<
custom1:SemanticRangeEnd
AnnotationID
=
"1"
/>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Hob: "
/>
<
custom1:SemanticRangeStart
AnnotationID
=
"2"
Name
=
"SemanticRange 1"
/>
<
custom1:SemanticRangeEnd
AnnotationID
=
"2"
/>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Electric: "
/>
<
custom1:SemanticRangeStart
AnnotationID
=
"3"
Name
=
"SemanticRange 2"
/>
<
custom1:SemanticRangeEnd
AnnotationID
=
"3"
/>
<
t:Span
FontFamily
=
"Verdana"
FontSize
=
"16"
FontStyle
=
"Normal"
FontWeight
=
"Normal"
Text
=
"Beko HIC64102"
/>
</
t:Paragraph
>