/// <summary> /// Sets a containers ZIndex. /// </summary> /// <param name="item">The text box item.</param> /// <param name="index">The new Z-Index.</param> public void SetContainerZIndex(DecoratorTextBoxItem item, int index) { if (this.containersByItem.ContainsKey(item)) { Canvas.SetZIndex(this.containersByItem[item], index); } }
/// <summary> /// Gets a container from the item. /// </summary> /// <param name="item">The text item.</param> /// <returns>The holding container.</returns> internal DecoratorItemContainer GetContainerFromItem(DecoratorTextBoxItem item) { if (this.containersByItem.ContainsKey(item)) { return(this.containersByItem[item]); } return(null); }
/// <summary> /// Removes an entry from the item-container dictionary. /// </summary> /// <param name="element">The item container.</param> /// <param name="item">The text item.</param> protected override void ClearContainerForItemOverride(DependencyObject element, object item) { base.ClearContainerForItemOverride(element, item); DecoratorTextBoxItem textItem = (DecoratorTextBoxItem)item; DecoratorItemContainer container = (DecoratorItemContainer)element; this.containers.Remove(container); if (textItem != null && this.containersByItem.ContainsKey(textItem)) { this.containersByItem.Remove(textItem); } }
/// <summary> /// Sets up the bindings for whether a term is encodable or not, and stores /// in the item-container dictionary. /// </summary> /// <param name="element">The decorator container.</param> /// <param name="item">The text item.</param> protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { base.PrepareContainerForItemOverride(element, item); DecoratorItemContainer container = (DecoratorItemContainer)element; DecoratorTextBoxItem textItem = (DecoratorTextBoxItem)item; this.containersByItem.Add(textItem, container); container.DataContext = textItem; container.SetBinding(DecoratorItemContainer.IsEncodableProperty, new System.Windows.Data.Binding("IsEncodable")); container.SetBinding(DecoratorItemContainer.IsEncodedProperty, new System.Windows.Data.Binding("IsEncoded")); container.SetBinding(DecoratorItemContainer.ContainerPositionProperty, new System.Windows.Data.Binding("ContainerPosition")); container.SetBinding(DecoratorItemContainer.IsMouseHighlightedProperty, new System.Windows.Data.Binding("IsMouseHighlighted")); container.SetBinding(DecoratorItemContainer.IsFocusHighlightedProperty, new System.Windows.Data.Binding("IsFocusHighlighted")); }
/// <summary> /// Changes the text for a specific term. /// <para> /// This method uses the start index (from term item) to move through the decorator textbox /// items, and update the text in the item to show the new text. If the new text is longer, /// more text box items are inserted. If the new text is shorter, text box items are removed. /// </para> /// <para> /// The text box text is then updated with the change, flagging that the change is a programmatic /// change. And finally, each term item that comes after the changed item, has the start index /// updated with its new start position. /// </para> /// </summary> /// <param name="termContainer">The term container to update.</param> internal void ChangeText(MatchingTermItemContainer termContainer) { if (termContainer != null) { TermItem termItem = this.termItemsControl.GetContainerFromItem(termContainer.DataContext); if (termItem != null) { if (this.textBox != null) { this.programaticTextChangeCount++; } int count = 0; bool mouseHighlighted = false; bool focusHighlighted = false; for (int i = termItem.StartIndex; i < Math.Min(termItem.StartIndex + termItem.Length, termItem.StartIndex + termContainer.SelectedItemText.Length); i++) { if (i == termItem.StartIndex) { focusHighlighted = this.textBoxItems[i].IsFocusHighlighted; mouseHighlighted = this.textBoxItems[i].IsMouseHighlighted; } this.textBoxItems[i].Text = termContainer.SelectedItemText[count].ToString(CultureInfo.CurrentCulture); count++; } if (termItem.Length < termContainer.SelectedItemText.Length) { if (termItem.Length > 1) { this.textBoxItems[termItem.StartIndex + termItem.Length - 1].ContainerPosition = ContainerPosition.Middle; } else { this.textBoxItems[termItem.StartIndex].ContainerPosition = ContainerPosition.Start; } for (int i = termItem.StartIndex + termItem.Length; i < termItem.StartIndex + termContainer.SelectedItemText.Length; i++) { DecoratorTextBoxItem textBoxItem = new DecoratorTextBoxItem(termContainer.SelectedItemText[i - termItem.StartIndex].ToString(CultureInfo.CurrentCulture)); if (i == termItem.StartIndex + termContainer.SelectedItemText.Length - 1) { textBoxItem.ContainerPosition = ContainerPosition.End; } else { textBoxItem.ContainerPosition = ContainerPosition.Middle; } textBoxItem.IsFocusHighlighted = focusHighlighted; textBoxItem.IsMouseHighlighted = mouseHighlighted; textBoxItem.AddTermItem(termItem); this.textBoxItems.Insert(i, textBoxItem); } } else if (termItem.Length > termContainer.SelectedItemText.Length) { for (int i = termItem.StartIndex + termContainer.SelectedItemText.Length; i < termItem.StartIndex + termItem.Length; i++) { this.textBoxItems.RemoveAt(termItem.StartIndex + termContainer.SelectedItemText.Length); } if (termContainer.SelectedItemText.Length > 1) { this.textBoxItems[termItem.StartIndex + termContainer.SelectedItemText.Length - 1].ContainerPosition = ContainerPosition.End; } else { this.textBoxItems[termItem.StartIndex].ContainerPosition = ContainerPosition.Only; } } if (this.textBox != null) { string newText = this.textBox.Text.Remove(termItem.StartIndex, termItem.Length); newText = newText.Insert(termItem.StartIndex, termContainer.SelectedItemText); this.textBox.Text = newText; } foreach (TermItem item in this.termItemsControl.TermItems) { if (item.StartIndex > termItem.StartIndex) { item.StartIndex += termContainer.SelectedItemText.Length - termItem.Length; } } termItem.Length = termContainer.SelectedItemText.Length; } } }