From 0499cd1b56815ef02bdfc30919b1ace3c0605898 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Tue, 11 Oct 2011 21:31:54 +0000 Subject: [PATCH] added new control TokenizedTextBox. Not ready for use yet, still working on architecture and funtionality. --- .../WPFToolkit.Extended/Themes/Generic.xaml | 3 +- .../TokenizedTextBox/Images/delete8.png | Bin 0 -> 312 bytes .../Implementation/TokenItem.cs | 20 ++ .../Implementation/TokenizedTextBox.cs | 276 ++++++++++++++++++ .../TokenizedTextBoxCommands.cs | 14 + .../TokenizedTextBox/Themes/Generic.xaml | 59 ++++ .../WPFToolkit.Extended.csproj | 10 + 7 files changed, 380 insertions(+), 2 deletions(-) create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Images/delete8.png create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Implementation/TokenItem.cs create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Implementation/TokenizedTextBox.cs create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Implementation/TokenizedTextBoxCommands.cs create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Themes/Generic.xaml diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml index 8700b78a..3bb76652 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml @@ -21,9 +21,8 @@ + - - diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Images/delete8.png b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Images/delete8.png new file mode 100644 index 0000000000000000000000000000000000000000..153a6c82b2e399c65bf056261c4ba5f10aa8d1c6 GIT binary patch literal 312 zcmV-80muG{P)pF>PbXFR2Y>_j87{>VH`%!?_HRTm=eB$OgExQvXIZANmndbn!BNf zx>o7AB!>NEKjGy^2(}mL79r83y{&2H^PP z`|A@kLlViBWJA}3Ff1sJWBkc!Iy$@9*)CFkeG`B?ySP#mRmltvk?HqraUpN5Z7H^j z>-EFw$uZ73{PC + { + var run = inline as Run; + return (run != null && run.Text.EndsWith(inputText)); + }) as Run; + + if (matchedRun != null) // Found a Run that matched the inputText + { + var tokenContainer = CreateTokenContainer(inputText, token); + para.Inlines.InsertBefore(matchedRun, tokenContainer); + + // Remove only if the Text in the Run is the same as inputText, else split up + if (matchedRun.Text == inputText) + { + para.Inlines.Remove(matchedRun); + } + else // Split up + { + var index = matchedRun.Text.IndexOf(inputText) + inputText.Length; + var tailEnd = new Run(matchedRun.Text.Substring(index)); + para.Inlines.InsertAfter(matchedRun, tailEnd); + para.Inlines.Remove(matchedRun); + } + + //now append the Text + SetTextInternal(Text + inputText); + } + + _surpressTextChangedEvent = false; + } + + private InlineUIContainer CreateTokenContainer(string tokenKey, object token) + { + return new InlineUIContainer(CreateTokenItem(tokenKey, token)) { BaselineAlignment = BaselineAlignment.Center }; + } + + private TokenItem CreateTokenItem(string tokenKey, object token) + { + var tokenItem = new TokenItem(); + tokenItem.TokenKey = tokenKey; + tokenItem.Content = token; + tokenItem.ContentTemplate = TokenTemplate; + + if (TokenTemplate == null) + { + //if no template was supplied let's try to get a value from the object using the DisplayMemberPath + if (!String.IsNullOrEmpty(DisplayMemberPath)) + { + var property = token.GetType().GetProperty(DisplayMemberPath); + if (property != null) + { + var value = property.GetValue(token, null); + if (value != null) + tokenItem.Content = value; + } + } + } + + return tokenItem; + } + + private void DeleteToken(object sender, ExecutedRoutedEventArgs e) + { + var para = _rtb.CaretPosition.Paragraph; + + Inline inlineToRemove = para.Inlines.SingleOrDefault(x => ((x as InlineUIContainer).Child as TokenItem).TokenKey.Equals(e.Parameter)); + + if (inlineToRemove != null) + para.Inlines.Remove(inlineToRemove); + + //update Text to remove delimited value + SetTextInternal(Text.Replace(e.Parameter.ToString(), "")); + } + + private void SetTextInternal(string text) + { + _surpressTextChanged = true; + Text = text; + _surpressTextChanged = false; + } + + #endregion //Methods + } +} \ No newline at end of file diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Implementation/TokenizedTextBoxCommands.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Implementation/TokenizedTextBoxCommands.cs new file mode 100644 index 00000000..89e05c5f --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Implementation/TokenizedTextBoxCommands.cs @@ -0,0 +1,14 @@ +using System.Windows.Input; + +namespace Microsoft.Windows.Controls +{ + public static class TokenizedTextBoxCommands + { + + private static RoutedCommand _deleteCommand = new RoutedCommand(); + public static RoutedCommand Delete + { + get { return _deleteCommand; } + } + } +} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Themes/Generic.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Themes/Generic.xaml new file mode 100644 index 00000000..ee422d5b --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/TokenizedTextBox/Themes/Generic.xaml @@ -0,0 +1,59 @@ + + + + + + + \ No newline at end of file diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj index c16ad8eb..85f40a74 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj @@ -159,6 +159,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -291,6 +295,9 @@ + + + @@ -376,6 +383,9 @@ + + +