private void OnRemoveOptionClick(object sender, RoutedEventArgs e) { ValidateDeleteButton(); if (OptionsCollection.Any() && IsSelectedItem()) { var index = expressionsListBox.SelectedIndex; OptionsCollection.RemoveAt(index); var count = OptionsCollection.Count; if (count > 0) { if (index >= count) { index = count - 1; } expressionsListBox.SelectedIndex = index; } else { if (!string.IsNullOrEmpty(_expressionEditor.ExpressionText)) { _expressionEditor.ExpressionText = string.Empty; } } } }