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);
        }
        /// <summary>
        /// Remove the specified token from the list.
        /// </summary>
        /// <param name="item">Item in the list to delete</param>
        /// <param name="data">data </param>
        /// <remarks>
        /// the data parameter is passed in optionally to support UX UTs. When running in the UT the Container items are not manifest.
        /// </remarks>
        /// <returns><b>true</b> if the item was removed successfully, <b>false</b> otherwise</returns>
        private async Task <bool> RemoveTokenAsync(TokenizingTextBoxItem item, object data = null)
        {
            if (data == null)
            {
                data = ItemFromContainer(item);
            }

            if (TokenItemRemoving != null)
            {
                var tirea = new TokenItemRemovingEventArgs(data, item);
                await TokenItemRemoving.InvokeAsync(this, tirea);

                if (tirea.Cancel)
                {
                    return(false);
                }
            }

            _innerItemsSource.Remove(data);

            TokenItemRemoved?.Invoke(this, data);

            GuardAgainstPlaceholderTextLayoutIssue();

            return(true);
        }
        private void UpdateTokensCounter(TokenizingTextBoxItem ttbi)
        {
            var maxTokensCounter = (TextBlock)_autoSuggestBox?.FindDescendant(PART_TokensCounter);

            if (maxTokensCounter == null)
            {
                return;
            }

            void OnTokenCountChanged(TokenizingTextBox ttb, object value = null)
            {
                var itemsSource   = ttb.ItemsSource as InterspersedObservableCollection;
                var currentTokens = itemsSource.ItemsSource.Count;
                var maxTokens     = ttb.MaximumTokens;

                maxTokensCounter.Text       = $"{currentTokens}/{maxTokens}";
                maxTokensCounter.Visibility = Visibility.Visible;

                maxTokensCounter.Foreground = (currentTokens >= maxTokens)
                    ? new SolidColorBrush(Colors.Red)
                    : _autoSuggestBox.Foreground;
            }

            ttbi.Owner.TokenItemAdded   -= OnTokenCountChanged;
            ttbi.Owner.TokenItemRemoved -= OnTokenCountChanged;

            if (Content is ITokenStringContainer str && str.IsLast && ttbi?.Owner != null && ttbi.Owner.ReadLocalValue(TokenizingTextBox.MaximumTokensProperty) != DependencyProperty.UnsetValue)
            {
                ttbi.Owner.TokenItemAdded   += OnTokenCountChanged;
                ttbi.Owner.TokenItemRemoved += OnTokenCountChanged;
                OnTokenCountChanged(ttbi.Owner);
            }
Esempio n. 4
0
        private void TokenizingTextBoxItem_ClearClicked(TokenizingTextBoxItem sender, RoutedEventArgs args)
        {
            bool removeMulti = false;

            foreach (var item in SelectedItemsInternal)
            {
                if (item == sender)
                {
                    removeMulti = true;
                    break;
                }
            }

            if (removeMulti)
            {
                while (SelectedItemsInternal.Count > 0)
                {
                    var b = SelectedItemsInternal[0] as TokenizingTextBoxItem;
                    RemoveToken(b);
                }
            }
            else
            {
                RemoveToken(sender);
            }
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        private void TokenizingTextBoxItem_KeyUp(object sender, KeyRoutedEventArgs e)
        {
            TokenizingTextBoxItem ttbi = sender as TokenizingTextBoxItem;

            switch (e.Key)
            {
            case VirtualKey.Left:
            {
                FocusManager.TryMoveFocus(FocusNavigationDirection.Left);
                break;
            }

            case VirtualKey.Right:
            {
                FocusManager.TryMoveFocus(FocusNavigationDirection.Right);
                break;
            }

            case VirtualKey.Up:
            {
                FocusManager.TryMoveFocus(FocusNavigationDirection.Up);
                break;
            }

            case VirtualKey.Down:
            {
                FocusManager.TryMoveFocus(FocusNavigationDirection.Down);
                break;
            }

            case VirtualKey.Space:
            {
                ttbi.IsSelected = !ttbi.IsSelected;
                break;
            }

            case VirtualKey.C:
            {
                var controlPressed = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
                if (controlPressed)
                {
                    CopySelectedToClipboard();
                }

                break;
            }
            }
        }
        private void ClearAllTextSelections(TokenizingTextBoxItem ignoreItem)
        {
            // Clear any selection in the text box
            foreach (var item in Items)
            {
                if (item is PretokenStringContainer)
                {
                    var container = ContainerFromItem(item) as TokenizingTextBoxItem;

                    if (container != ignoreItem)
                    {
                        container._autoSuggestTextBox.SelectionLength = 0;
                    }
                }
            }
        }
        private bool SelectNewItem(TokenizingTextBoxItem item, int increment, Func <int, bool> testFunc)
        {
            bool returnVal = false;

            // find the item in the list
            var currentIndex = IndexFromContainer(item);

            // Select previous token item (if there is one).
            if (testFunc(currentIndex))
            {
                var newItem = ContainerFromItem(Items[currentIndex + increment]) as ListViewItem;
                newItem.Focus(FocusState.Keyboard);
                SelectedItems.Add(Items[currentIndex + increment]);
                returnVal = true;
            }

            return(returnVal);
        }
Esempio n. 9
0
        private async Task RemoveToken(TokenizingTextBoxItem item)
        {
            var data = ItemFromContainer(item);

            if (TokenItemRemoving != null)
            {
                var tirea = new TokenItemRemovingEventArgs(data, item);
                await TokenItemRemoving.InvokeAsync(this, tirea);

                if (tirea.Cancel)
                {
                    return;
                }
            }

            _innerItemsSource.Remove(data);

            TokenItemRemoved?.Invoke(this, data);
        }
        private async void TokenizingTextBoxItem_ClearAllAction(TokenizingTextBoxItem sender, RoutedEventArgs args)
        {
            // find the first item selected
            int newSelectedIndex = -1;

            if (SelectedRanges.Count > 0)
            {
                newSelectedIndex = SelectedRanges[0].FirstIndex - 1;
            }

            await RemoveAllSelectedTokens();

            SelectedIndex = newSelectedIndex;

            if (newSelectedIndex == -1)
            {
                newSelectedIndex = Items.Count - 1;
            }

            // focus the item prior to the first selected item
            (ContainerFromIndex(newSelectedIndex) as TokenizingTextBoxItem).Focus(FocusState.Keyboard);
        }
        private void RemoveToken(TokenizingTextBoxItem item)
        {
            var tirea = new TokenItemRemovedEventArgs(item?.Content, item);

            TokenItemRemoved?.Invoke(this, tirea);

            if (tirea.Cancel)
            {
                return;
            }

            SelectedItems.Remove(item.Content);

            var itemIndex = Math.Max(_wrapPanel.Children.IndexOf(item) - 1, 0);

            _wrapPanel.Children.Remove(item);

            if (_wrapPanel.Children[itemIndex] is Control control)
            {
                control.Focus(FocusState.Programmatic);
            }
        }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TokenItemRemovingEventArgs"/> class.
 /// </summary>
 /// <param name="item">Item being removed.</param>
 /// <param name="token"><see cref="TokenizingTextBoxItem"/> container being closed.</param>
 public TokenItemRemovingEventArgs(object item, TokenizingTextBoxItem token)
 {
     Item  = item;
     Token = token;
 }
 private async void TokenizingTextBoxItem_ClearClicked(TokenizingTextBoxItem sender, RoutedEventArgs args)
 {
     await RemoveTokenAsync(sender);
 }
 private void TokenizingTextBoxItem_ClearClicked(TokenizingTextBoxItem sender, RoutedEventArgs args)
 {
     RemoveToken(sender);
 }
 /// <summary>
 /// Select the next item in the list, if one is available. Called when moving from textbox to token.
 /// </summary>
 /// <param name="item">identifies the current item</param>
 /// <returns>a value indicating whether the next item was successfully selected, false if nothing was changed</returns>
 internal bool SelectNextItem(TokenizingTextBoxItem item)
 {
     return(SelectNewItem(item, 1, i => i < Items.Count - 1));
 }
 /// <summary>
 /// Select the previous item in the list, if one is available. Called when moving from textbox to token.
 /// </summary>
 /// <param name="item">identifies the current item</param>
 /// <returns>a value indicating whether the previous item was successfully selected</returns>
 internal bool SelectPreviousItem(TokenizingTextBoxItem item)
 {
     return(SelectNewItem(item, -1, i => i > 0));
 }
 internal void DeselectAllTokensAndText(TokenizingTextBoxItem ignoreItem = null)
 {
     this.DeselectAll();
     ClearAllTextSelections(ignoreItem);
 }