/// <summary>
        /// Removes a tag from the collection
        /// </summary>
        internal void RemoveTag(TokenizedTagItem tag, bool cancelEvent = false)
        {
            if (this.ItemsSource != null)
            {
                ((IList)this.ItemsSource).Remove(tag); // assume IList for convenience
                this.Items.Refresh();
                //UpdateAllTagsProperty();

                if (TagRemoved != null && !cancelEvent)
                {
                    TagRemoved(this, new TokenizedTagEventArgs(tag));
                }

                // select the last item
                if (this.SelectedItem == null && this.Items.Count > 0)
                {
                    //TokenizedTagItem itemToSelect = this.Items.GetItemAt(0) as TokenizedTagItem;
                    TokenizedTagItem itemToSelect = Items.GetItemAt(Items.Count - 1) as TokenizedTagItem;
                    if (!object.ReferenceEquals(itemToSelect, null))
                    {
                        RaiseTagClick(itemToSelect);
                        if (this.IsSelectable)
                        {
                            this.SelectedItem = itemToSelect;
                        }
                    }
                    //this.SelectedItem = this.Items.CurrentItem;
                }
            }
        }
        /// <summary>
        /// Adds a tag to the collection
        /// </summary>
        internal void AddTag(TokenizedTagItem tag)
        {
            TokenizedTagItem itemToSelect = null;

            if (this.SelectedItem == null && this.Items.Count > 0)
            {
                itemToSelect = (TokenizedTagItem)this.SelectedItem;
            }
            ((IList)this.ItemsSource).Add(tag); // assume IList for convenience
            this.Items.Refresh();

            // select the previous item
            if (!object.ReferenceEquals(itemToSelect, null))
            {
                // && !object.ReferenceEquals(TagApplied, null)
                //TagApplied.Invoke(this, new TokenizedTagEventArgs(appliedTag));
                RaiseTagClick(itemToSelect);
                if (this.IsSelectable)
                {
                    this.SelectedItem = itemToSelect;
                }
            }

            // update values
            //UpdateAllTagsProperty();
            TagAdded?.Invoke(this, new TokenizedTagEventArgs(tag));
        }
        /// <summary>
        /// Raises the TagDoubleClick event
        /// </summary>
        internal void RaiseTagDoubleClick(TokenizedTagItem tag)
        {
            //if (this.IsSelectable)
            //    this.SelectedItem = tag;

            UpdateAllTagsProperty();
            tag.IsEditing = true;
        }
 /// <summary>
 /// Raises the TagClick event
 /// </summary>
 internal void RaiseTagApplied(TokenizedTagItem tag)
 {
     /*
      * if (this.IsSelectable)
      *  this.SelectedItem = tag;
      */
     TagApplied?.Invoke(this, new TokenizedTagEventArgs(tag));
 }
 /// <summary>
 /// Raises the TagClick event
 /// </summary>
 internal void RaiseTagApplied(TokenizedTagItem tag)
 {
     /*
      * if (this.IsSelectable)
      *  this.SelectedItem = tag;
      */
     if (this.TagApplied != null)
     {
         TagApplied(this, new TokenizedTagEventArgs(tag));
     }
 }
        internal TokenizedTagItem InitializeNewTag(bool suppressEditing = false)
        {
            var newItem = new TokenizedTagItem()
            {
                IsEditing = !suppressEditing
            };

            AddTag(newItem);
            UpdateAllTagsProperty();
            this.IsEditing = !suppressEditing;

            return(newItem);
        }
        public void OnApplyTemplate(TokenizedTagItem appliedTag = null)
        {
            Button createBtn = this.GetTemplateChild("PART_CreateTagButton") as Button;

            if (createBtn != null)
            {
                createBtn.Click -= createBtn_Click;
                createBtn.Click += createBtn_Click;
                //createBtn.Focus();
                // nixin - focuses
                //createBtn_Click(createBtn, null);
            }

            base.OnApplyTemplate();

            if (appliedTag != null && !object.ReferenceEquals(TagApplied, null))
            {
                TagApplied.Invoke(this, new TokenizedTagEventArgs(appliedTag));
            }
        }
        void TokenizedTagControl_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            if (!IsSelectable)
            {
                this.SelectedItem = null;
                return;
            }

            TokenizedTagItem itemToSelect = null;

            if (this.Items.Count > 0 && !object.ReferenceEquals((TokenizedTagItem)this.Items.CurrentItem, null))
            {
                if (this.SelectedItem != null && ((TokenizedTagItem)this.SelectedItem).Text != null &&
                    !((TokenizedTagItem)this.SelectedItem).IsEditing)
                {
                    itemToSelect = (TokenizedTagItem)this.SelectedItem;
                }
                else if (!String.IsNullOrWhiteSpace(((TokenizedTagItem)this.Items.CurrentItem).Text))
                {
                    itemToSelect = (TokenizedTagItem)this.Items.CurrentItem;
                }
            }

            // select the previous item
            if (!object.ReferenceEquals(itemToSelect, null))
            {
                e.Handled = true;
                RaiseTagApplied(itemToSelect);
                if (this.IsSelectable)
                {
                    this.SelectedItem = itemToSelect;
                    //itemToSelect.Focus();
                }
            }
            //RaiseTagClick(itemToSelect);
        }
 public TokenizedTagEventArgs(TokenizedTagItem item)
 {
     this.Item = item;
 }