I tried these methods.
private void EditorPreviewEditorKeyDown(object sender, PreviewEditorKeyEventArgs e) {var word = radRichTextBox.Document.CaretPosition.GetCurrentWord();
or... var word = radRichTextBox.Document.CaretPosition.GetCurrentSpanBox().Text;.
But none of them gives me the desired result.
Do they work in Wpf? What do they do? How could I get the word 'at the caret'?
Where can I read (wpf)documentation about this?
7 Answers, 1 is accepted
You can use DocumentPositions In order to get the last word you have typed:
DocumentPosition endPosition =
new
DocumentPosition(
this
.radRichTextBox.Document.CaretPosition);
DocumentPosition startPosition =
new
DocumentPosition(endPosition);
startPosition.MoveToCurrentWordStart();
this
.radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
this
.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
string
lastWord =
this
.radRichTextBox.Document.Selection.GetSelectedText();
this
.radRichTextBox.Document.Selection.Clear();
When it comes to the approach you have chosen - it does not work for two reasons:
1. There is no direct parallel between SpanLayoutBox and a word. For example, the text in one paragraph is split into Spans according to the formatting. After that, each Span is split into SpanLayoutBoxes at the places where you have formatting symbols (line break, interval, etc.) or digits. For example, if you have "word1" in the document, there will be 1 Span, but 2 SpanLayoutBoxes - "word" and "1" respectively. If there is different formatting within the same word, the Text of the SpanLayoutBox will also not represent the whole world.
2. The GetCurrentSpanBox() method of DocumentPosition returns the SpanLayoutBox that the position is in or the next SpanLayoutBox if the position is between two layout boxes. In your case, if you are at the end of the word, it will return the next SpanLayoutBox (which probably has an interval as Text).
I hope this helps.
Kind regards,
Martin
the Telerik team
It works fine, but do I have to select first before I can get the last word?
And what does the radRichTextBox.Document.CaretPosition.GetCurrentWord() do?
When and where could this be used?
The GetCurrentWord method of DocumentPosition returns the word that the position is in. If the caret is between two words, e.g. "first |second", it will return "second". However, if it is just after "first" ("first| second"), it will return an interval " ". Similarly, if the word is at the end of the document "last|", the current word will be "\r\n", what stands for a new pararagraph or the end of the document.
Considering the fact that you want to get the word that has just been typed, you will probably fall in this case and the GetCurrentWord method will return "\r\n". You are right though, that you can use this method, only adjust the position if needed like this:
DocumentPosition positionInDocument =
new
DocumentPosition(
this
.editor.Document.CaretPosition);
if
(
this
.editor.Document.CaretPosition.GetCurrentSpanBox().IsFormattingSymbol)
{
positionInDocument.MoveToPrevious();
}
MessageBox.Show(positionInDocument.GetCurrentWord());
Iva Toteva
the Telerik team
Is there a class method to get all words within a span?
For example, I have the following:
<
telerik:Span
Text
=
"Doors and Drawers: "
></
telerik:Span
>
When I use the following code I only get one of the words such as "Doors", "and" or "Drawers". However I want it to return "Doors and Drawers".
string
currentSpanText =
this
.radRichTextBox1.Document.CaretPosition.GetCurrentSpanBox().Text;
Thank you very much for your time,
Rob
If you want to obtain the text of the whole span, you can use the AssociatedSpan property of SpanLayoutBox in the following way:
string
currentSpanText =
this
.radRichTextBox1.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan.Text;
Note that if you allow images and annotations in the document, GetCurrentSpanBox() will return null, if the caret is positioned just before an image or an annotation. Therefore, you may need to add a check for that.
Greetings,
Iva Toteva
the Telerik team
One last question. It seems that spans merge at runtime unless they have different properities such as font size. Is it possible to stop spans from merging even if their properties are the same?
For example:
<
t:Paragraph
>
<
t:Span
Text
=
"Hello world! "
></
t:Span
>
<
t:Span
Text
=
"Hello Rob!"
></
t:Span
>
</
t:Paragraph
>
will produce a string of "Hello World! Hello Rob!"
But if I alter a property in one of them such as font size:
<
t:Paragraph
>
<
t:Span
Text
=
"Hello world! "
></
t:Span
>
<
t:Span
FontSize
=
"20"
Text
=
"Hello Rob!"
></
t:Span
>
</
t:Paragraph
>
I get two strings. One is "Hello world! " and another "Hello Rob!".
I want the effect of two seperate spans / strings like the last example but I don't want to have to alter a property in order to achieve it.
Is there any way to achieve this?
Thank you again for your time.
Spans with equal properties cannot be prevented from merging. This is so because spans are only concerned with formatting and aren't meant to be used when preserving semantic or structural content is needed.
However, we are currently preparing a blog post specifically on storing semantic information. I'm attaching a work-in-progress version of it here, so you can have a look at it. You can also check the demo for Silverlight at ...
Let us know if you need additional information.
Regards,
Ivailo Karamanolev
the Telerik team