Browse Source

TokenizedTextBox: handling proper deletion of tokens using the Back and Delete keys.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
b7a284dd99
  1. 45
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Implementation/TokenizedTextBox.cs

45
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Implementation/TokenizedTextBox.cs

@ -99,10 +99,16 @@ namespace Microsoft.Windows.Controls
base.OnApplyTemplate(); base.OnApplyTemplate();
if (_rtb != null) if (_rtb != null)
_rtb.TextChanged -= OnTextChanged; {
_rtb.TextChanged -= RichTextBox_TextChanged;
_rtb.PreviewKeyDown -= RichTextBox_PreviewKeyDown;
}
_rtb = GetTemplateChild("PART_ContentHost") as System.Windows.Controls.RichTextBox; _rtb = GetTemplateChild("PART_ContentHost") as System.Windows.Controls.RichTextBox;
if (_rtb != null) if (_rtb != null)
_rtb.TextChanged += OnTextChanged; {
_rtb.TextChanged += RichTextBox_TextChanged;
_rtb.PreviewKeyDown += RichTextBox_PreviewKeyDown;
}
InitializeTokensFromText(); InitializeTokensFromText();
} }
@ -111,7 +117,7 @@ namespace Microsoft.Windows.Controls
#region Event Handlers #region Event Handlers
private void OnTextChanged(object sender, TextChangedEventArgs e) private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
{ {
if (_surpressTextChangedEvent) if (_surpressTextChangedEvent)
return; return;
@ -124,6 +130,39 @@ namespace Microsoft.Windows.Controls
} }
} }
void RichTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
InlineUIContainer container = null;
if (e.Key == Key.Back)
{
container = _rtb.CaretPosition.GetAdjacentElement(LogicalDirection.Backward) as InlineUIContainer;
}
else if (e.Key == Key.Delete)
{
//if the selected text is a blank space, I will assume that a token item is selected.
//if a token item is selected, we need to move the caret position to the left of the element so we can grab the InlineUIContainer
if (_rtb.Selection.Text == " ")
{
TextPointer moveTo = _rtb.CaretPosition.GetNextInsertionPosition(LogicalDirection.Backward);
_rtb.CaretPosition = moveTo;
}
//the cursor is to the left of a token item
container = _rtb.CaretPosition.GetAdjacentElement(LogicalDirection.Forward) as InlineUIContainer;
}
//if the container is not null that means we have something to delete
if (container != null)
{
var token = (container as InlineUIContainer).Child as TokenItem;
if (token != null)
{
SetTextInternal(Text.Replace(token.TokenKey, ""));
}
}
}
#endregion //Event Handlers #endregion //Event Handlers
#region Methods #region Methods

Loading…
Cancel
Save