Aug 30, 2010 at 7:43 AM
Edited Aug 30, 2010 at 7:46 AM

I need to allow users to clear only their own highlights(annotations), and not another users highlights.

In my implementation each annotation has only one Author.

As far as I know : it seems there is a logic which maps text selection to actual Highlight annotation object, and decides to remove or change StartTextIndex andEndtextIndex.

How can I implement this? Seems the code inside ClearHighlightCommand is the key!


Aug 30, 2010 at 8:29 AM
Edited Aug 31, 2010 at 8:29 AM

The below snippet removes all highlights currently selected. It should be easy to add your own logic


public void RemoveHighlightsForSelection(AnnotationService service)
  if (!service.TextContainer.Selection.IsEmpty) {
    // ensure AnnotationStore doesn't raise a zillion Changed events

    RemoveHighlights(service.Store, service.TextContainer.Selection);

    // raises the Changed event one time

private void RemoveHighlights(AnnotationStore store, TextRange range)
  // removes all highlights in the given range
  foreach (var highlight in store.OfType<Highlight>().ToArray()) {
    if (highlight.TextRanges.Clip(range)) {
      // remove entire highlight when its text range collection is empty.
      if (highlight.TextRanges.IsEmpty()) {



Aug 31, 2010 at 1:51 AM

I implemented the OnExecute metohd of the derived  ClearHighlightcommand, and it worked fine.

Ive fidex this,Just FYI : foreach block and store.Remove lines throw an exception : "Collection was modified; enumeration operation may not execute."

Thanks for your help

Aug 31, 2010 at 8:29 AM

You are correct, I've modified the sample code so it no longer throws.