private void TagControl_OnPreviewMouseUp(object sender, MouseButtonEventArgs e) { TagControlItemViewModel itemWithEDit = null; foreach (var item in lst.Items) { if ((item as TagControlItemViewModel).IsEditing) { itemWithEDit = item as TagControlItemViewModel; } } if (itemWithEDit != null) { var item = lst.ItemContainerGenerator.ContainerFromItem(itemWithEDit) as ListBoxItem; if (item != null) { item.Focus(); var txt = UIHelper.FindVisualChild <TextBox>(item); if (txt != null) { txt.Focus(); } } } }
private void BackSpacePressed(string inputText) { if (string.IsNullOrEmpty(inputText)) { var lastItem = TagViewModelCollection.LastOrDefault(tvm => tvm.IsEditing == false); if (lastItem != null) { TagViewModelCollection.Remove(lastItem); } } else { TagControlItemViewModel itemWithEDit = null; foreach (var item in lst.Items) { if ((item as TagControlItemViewModel).IsEditing) { itemWithEDit = item as TagControlItemViewModel; } } if (itemWithEDit != null) { var item = lst.ItemContainerGenerator.ContainerFromItem(itemWithEDit) as ListBoxItem; if (item != null) { var txt = UIHelper.FindVisualChild <TextBox>(item); if (txt != null) { string currentValue = txt.Text; string selectedText = txt.SelectedText; if (!string.IsNullOrEmpty(selectedText)) { //delete all selected text var caretIndex = txt.CaretIndex; txt.Text = txt.Text.Replace(txt.SelectedText, string.Empty); txt.CaretIndex = caretIndex; } else { //delete last symbol in textbox var caretIndex = txt.CaretIndex; if (caretIndex > 0) { string backSpace = currentValue.Remove(caretIndex - 1, 1); txt.Text = backSpace; txt.CaretIndex = caretIndex - 1; } } } } } } }