15 May 2017 12:34
RichTextBox: Changing the span style doesn't preserve the field range end when it is last element in paragraph
Undoing the action could lead to InvalidCastException, NullReferenceException, and other unexpected behaviors in next interactions with the document.
When the users didn't create a selection but rely on the caret position, the command creates selection on the current inline layout box internally. The selection's end position is on the range end, but it (the end) is not included in the actual selection. When the users try to undo, the range end is missing, which leads to incorrect visualization and different exceptions depending on the actions (when saving the document, when deleting the content, etc.).
Workaround: In CommandExecuting, check whether the selection is empty and create one if so. If the selection's end position is on an annotation range end, move the position to the next one. Clear the selection on CommandExecuted. Code is attached.