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); _currentTextEdit = _lastTextEdit = new PretokenStringContainer(true); _innerItemsSource.Insert(_innerItemsSource.Count, _currentTextEdit); ItemsSource = _innerItemsSource; } }
/// <summary> /// Initializes a new instance of the <see cref="TokenizingTextBox"/> class. /// </summary> public TokenizingTextBox() { // Setup our base state of our collection _innerItemsSource = new InterspersedObservableCollection(new ObservableCollection <object>()); // TODO: Test this still will let us bind to ItemsSource in XAML? _currentTextEdit = _lastTextEdit = new PretokenStringContainer(true); _innerItemsSource.Insert(_innerItemsSource.Count, _currentTextEdit); ItemsSource = _innerItemsSource; //// TODO: Consolidate with callback below for ItemsSourceProperty changed? DefaultStyleKey = typeof(TokenizingTextBox); // TODO: Do we want to support ItemsSource better? Need to investigate how that works with adding... RegisterPropertyChangedCallback(ItemsSourceProperty, ItemsSource_PropertyChanged); PreviewKeyDown += TokenizingTextBox_PreviewKeyDown; PreviewKeyUp += TokenizingTextBox_PreviewKeyUp; CharacterReceived += TokenizingTextBox_CharacterReceived; ItemClick += TokenizingTextBox_ItemClick; }
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; } }