private void UpdateAnnotations() { var selectedAnnotations = new HashSet <string>(SelectedAnnotationNames); var annotationTargets = SelectedHandlers.Aggregate(AnnotationDef.AnnotationTargetSet.EMPTY, (value, handler) => value.Union(handler.AnnotationTargets)); var newAnnotations = Document.Settings.DataSettings.AnnotationDefs.Where( annotationDef => annotationDef.AnnotationTargets.Intersect(annotationTargets).Any()) .Select(annotationDef => annotationDef.Name).OrderBy(name => name).ToArray(); if (newAnnotations.SequenceEqual(listBoxAnnotations.Items.OfType <string>())) { return; } listBoxAnnotations.Items.Clear(); listBoxAnnotations.Items.AddRange(newAnnotations); for (int i = 0; i < listBoxAnnotations.Items.Count; i++) { if (selectedAnnotations.Contains(newAnnotations[i])) { listBoxAnnotations.SelectedIndices.Add(i); } else { listBoxAnnotations.SelectedIndices.Remove(i); } } }