Hello Chris,
Currently RadRichTextBox does not fully support your scenario. We will take it into consideration in the future, but as of now, a little modification of your code solves the issues you have encountered:
public
void
InsertAnnotation(String noteText)
{
string
url =
"/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/FindNextSpellingError.png"
;
Image annotationIcon =
new
Image();
annotationIcon.Source =
new
BitmapImage(
new
Uri(url, UriKind.RelativeOrAbsolute));
CustomHyperLinkButton link =
new
CustomHyperLinkButton { Width = 20, Height = 20, Content = annotationIcon, NoteContent = noteText, };
link.Click += ShowText;
InlineUIContainer container =
new
InlineUIContainer
{
UiElement = link,
Width = 20,
Height = 20d,
};
editor.InsertInline(container);
}
private
void
ShowText(
object
sender, RoutedEventArgs e)
{
CustomHyperLinkButton pressedButton = sender
as
CustomHyperLinkButton;
List<InlineUIContainer> links = editor.Document.EnumerateChildrenOfType<InlineUIContainer>().ToList();
foreach
(var container
in
links)
{
CustomHyperLinkButton btn = container.UiElement
as
CustomHyperLinkButton;
if
(btn == pressedButton )
{
Span span =
new
Span(
"{ "
+ btn.NoteContent +
" }"
);
var inlineBox = container.FirstLayoutBox
as
InlineLayoutBox;
if
(inlineBox !=
null
)
{
editor.Document.CaretPosition.MoveToInline(inlineBox, 0);
editor.Document.InsertInline(span);
}
editor.Document.CaretPosition.MoveToNextSpanBox();
btn.Visibility = Visibility.Collapsed;
container.Height = 0;
container.Width = 0;
// this.editor.Delete(true); //if you want to delete the InlineUIContainer with the button
}
this
.editor.UpdateEditorLayout();
//this is needed only if you set the container.Height and Width properties
}
}
Note that DocumentPosition is a reference type and
creates an alias for the CaretPosition and it changes when the caret position moves. What you need most probably is:
Saving the DocumentPosition when creating the CustomHyperLinkButton is also a tricky moment, as this DocumentPosition will not be anchored to the same position in the document when the document content changes.
Try this approach and let us know if this solution does not match your requirements or you experience any further difficulties.
All the best,
Iva
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it.
Learn more>>