private void AddToken(object data) { var item = new TokenizingTextBoxItem() { Content = data, ContentTemplateSelector = TokenItemTemplateSelector, ContentTemplate = TokenItemTemplate, Style = TokenItemStyle }; item.Click += TokenizingTextBoxItem_Click; // TODO: Wonder if this needs to be in a PrepareContainerForItemOverride? item.ClearClicked += TokenizingTextBoxItem_ClearClicked; var removeMenuItem = new MenuFlyoutItem { Text = StringExtensions.GetLocalized("WindowsCommunityToolkit_TokenizingTextBoxItem_MenuFlyout_Remove", "Microsoft.Toolkit.Uwp.UI.Controls/Resources") }; removeMenuItem.Click += (s, e) => RemoveToken(item); var menuFlyout = new MenuFlyout(); menuFlyout.Items.Add(removeMenuItem); item.ContextFlyout = menuFlyout; var i = _wrapPanel.Children.Count - 1; _wrapPanel.Children.Insert(i, item); TokenItemAdded?.Invoke(this, item); }
internal async Task AddTokenAsync(object data, bool?atEnd = null) { if (ReadLocalValue(MaximumTokensProperty) != DependencyProperty.UnsetValue && (MaximumTokens <= 0 || MaximumTokens <= _innerItemsSource.ItemsSource.Count)) { // No tokens for you return; } if (data is string str && TokenItemAdding != null) { var tiaea = new TokenItemAddingEventArgs(str); await TokenItemAdding.InvokeAsync(this, tiaea); if (tiaea.Cancel) { return; } if (tiaea.Item != null) { data = tiaea.Item; // Transformed by event implementor } } // If we've been typing in the last box, just add this to the end of our collection if (atEnd == true || _currentTextEdit == _lastTextEdit) { _innerItemsSource.InsertAt(_innerItemsSource.Count - 1, data); } else { // Otherwise, we'll insert before our current box var edit = _currentTextEdit; var index = _innerItemsSource.IndexOf(edit); // Insert our new data item at the location of our textbox _innerItemsSource.InsertAt(index, data); // Remove our textbox _innerItemsSource.Remove(edit); } // Focus back to our end box as Outlook does. var last = ContainerFromItem(_lastTextEdit) as TokenizingTextBoxItem; last?._autoSuggestTextBox.Focus(FocusState.Keyboard); TokenItemAdded?.Invoke(this, data); GuardAgainstPlaceholderTextLayoutIssue(); }
private async Task AddToken(object data) { if (data is string str && TokenItemCreating != null) { var ticea = new TokenItemCreatingEventArgs(str); await TokenItemCreating.InvokeAsync(this, ticea); if (ticea.Cancel) { return; } if (ticea.Item != null) { data = ticea.Item; // Transformed by event implementor } } var item = new TokenizingTextBoxItem() { Content = data, ContentTemplateSelector = TokenItemTemplateSelector, ContentTemplate = TokenItemTemplate, Style = TokenItemStyle }; item.Click += TokenizingTextBoxItem_Click; // TODO: Wonder if this needs to be in a PrepareContainerForItemOverride? item.ClearClicked += TokenizingTextBoxItem_ClearClicked; item.KeyUp += TokenizingTextBoxItem_KeyUp; var removeMenuItem = new MenuFlyoutItem { Text = StringExtensions.GetLocalized("WindowsCommunityToolkit_TokenizingTextBoxItem_MenuFlyout_Remove", "Microsoft.Toolkit.Uwp.UI.Controls/Resources") }; removeMenuItem.Click += (s, e) => TokenizingTextBoxItem_ClearClicked(item, null); var menuFlyout = new MenuFlyout(); menuFlyout.Items.Add(removeMenuItem); item.ContextFlyout = menuFlyout; var i = _wrapPanel.Children.Count - 1; _wrapPanel.Children.Insert(i, item); TokenizedItemsInternal.Add(item); TokenItemAdded?.Invoke(this, item); }