private void TokenizingTextBoxItem_LostFocus(object sender, RoutedEventArgs e) { // Keep track of our currently focused textbox if (sender is TokenizingTextBoxItem ttbi && ttbi.Content is PretokenStringContainer text && string.IsNullOrWhiteSpace(text.Text) && text != _lastTextEdit) { // We're leaving an inner textbox that's blank, so we'll remove it _innerItemsSource.Remove(text); UpdateCurrentTextEdit(_lastTextEdit); } }
private void ItemsSource_PropertyChanged(DependencyObject sender, DependencyProperty dp) { // If we're given a different ItemsSource, we need to wrap that collection in our helper class. if (ItemsSource != null && ItemsSource.GetType() != typeof(InterspersedObservableCollection)) { _innerItemsSource = new InterspersedObservableCollection(ItemsSource); if (ReadLocalValue(MaximumTokensProperty) != DependencyProperty.UnsetValue && _innerItemsSource.ItemsSource.Count >= MaximumTokens) { // Reduce down to below the max as necessary. var endCount = MaximumTokens > 0 ? MaximumTokens : 0; for (var i = _innerItemsSource.ItemsSource.Count - 1; i >= endCount; --i) { _innerItemsSource.Remove(_innerItemsSource[i]); } } _currentTextEdit = _lastTextEdit = new PretokenStringContainer(true); _innerItemsSource.Insert(_innerItemsSource.Count, _currentTextEdit); ItemsSource = _innerItemsSource; } }