This question is locked. New answers and comments are not allowed.
I'm trying to create an extended control, which will display tips for writing. And everything is good, but the last step was faced with a problem.
Typing more then one word causes selection like "\r\n".
For example, type "Lorem" - it's ok. But type "Lorem " (with space) - broken.
01.
public
class
ImprovedRichTextBox : RadRichTextBox
02.
{
03.
private
static
readonly
DependencyProperty IsSuggestionConsistentProperty =
04.
DependencyProperty.Register(
05.
"IsSuggestionConsistent"
,
06.
typeof
(
bool
),
07.
typeof
(ImprovedRichTextBox),
08.
new
PropertyMetadata(
default
(
bool
))
09.
);
10.
11.
public
bool
IsSuggestionConsistent
12.
{
13.
get
{
return
(
bool
)GetValue(IsSuggestionConsistentProperty); }
14.
set
{ SetValue(IsSuggestionConsistentProperty, value); }
15.
}
16.
17.
private
DocumentPosition _startPosition;
18.
private
DocumentPosition _previousPosition;
19.
20.
// My custom added functionality
21.
protected
override
void
OnDocumentContentChanged()
22.
{
23.
base
.OnDocumentContentChanged();
24.
25.
var position =
new
DocumentPosition(Document.CaretPosition);
26.
27.
var back =
new
DocumentPosition(position);
28.
29.
if
(!back.MoveToPrevious())
30.
{
31.
IsSuggestionConsistent =
false
;
32.
return
;
33.
}
34.
35.
if
(IsSuggestionConsistent)
36.
{
37.
var i = _previousPosition.CompareTo(back);
38.
39.
var end =
new
DocumentPosition(position);
40.
end.MoveToLastPositionInDocument();
41.
42.
if
(i != 0 && (end.CompareTo(position) != 0 || end.CompareTo(_previousPosition) != 0))
43.
{
44.
IsSuggestionConsistent =
false
;
45.
}
46.
}
47.
48.
_previousPosition =
new
DocumentPosition(position);
49.
50.
if
(!IsSuggestionConsistent)
51.
{
52.
_startPosition =
new
DocumentPosition(back);
53.
IsSuggestionConsistent =
true
;
54.
}
55.
56.
var selection = Document.Selection;
57.
58.
// KEY feature: begin
59.
var selectionStart = _startPosition;
60.
61.
// KEY feature: actual end of typing phrase
62.
var selectionEnd = position;
63.
64.
//
65.
// The problem area is here:
66.
//
67.
68.
selection.SetSelectionStart(selectionStart);
69.
selection.AddSelectionEnd(selectionEnd);
70.
71.
// Everything is good while typing a word like "Lorem"
72.
//
73.
// But it will break if you put a space... Actually, it should be "Lorem ipsum", but catches only "\r\n"
74.
75.
Debug.WriteLine(
"|"
+ selection.GetSelectedText() +
"|"
);
76.
selection.Clear();
77.
}
78.
}