Skip to content

Commit 81975b0

Browse files
Merge pull request #3247 from windows-toolkit/marcpe/TokenizeTextBox
Add the TokenizingTextBox control behaviours for initial commit
2 parents 3910c2e + 68a59f0 commit 81975b0

32 files changed

+5565
-1011
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@
537537
<DependentUpon>StaggeredLayoutPage.xaml</DependentUpon>
538538
</Compile>
539539
<Compile Include="SamplePages\TokenizingTextBox\SampleDataType.cs" />
540+
<Compile Include="SamplePages\TokenizingTextBox\SampleEmailDataType.cs" />
540541
<Compile Include="SamplePages\TokenizingTextBox\TokenizingTextBoxPage.xaml.cs">
541542
<DependentUpon>TokenizingTextBoxPage.xaml</DependentUpon>
542543
</Compile>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/TokenizingTextBox/SampleDataType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class SampleDataType
2323

2424
public override string ToString()
2525
{
26-
return "Sample Data: " + Text;
26+
return Text;
2727
}
2828
}
29-
}
29+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Toolkit.Uwp.UI.Controls;
6+
using Windows.UI.Xaml.Controls;
7+
8+
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
9+
{
10+
/// <summary>
11+
/// Sample of strongly-typed email address simulated data for <see cref="TokenizingTextBox"/>.
12+
/// </summary>
13+
public class SampleEmailDataType
14+
{
15+
/// <summary>
16+
/// Gets or sets symbol to display.
17+
/// </summary>
18+
public Symbol Icon { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the first name .
22+
/// </summary>
23+
public string FirstName { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the family name .
27+
/// </summary>
28+
public string FamilyName { get; set; }
29+
30+
/// <summary>
31+
/// Gets the display text.
32+
/// </summary>
33+
public string DisplayName
34+
{
35+
get
36+
{
37+
return string.Format("{0} {1}", FirstName, FamilyName);
38+
}
39+
}
40+
41+
/// <summary>
42+
/// Gets the formatted email address
43+
/// </summary>
44+
public string EmailAddress
45+
{
46+
get
47+
{
48+
return string.Format("{0} <{1}.{2}@contoso.com>", DisplayName, FirstName, FamilyName);
49+
}
50+
}
51+
52+
public override string ToString()
53+
{
54+
return EmailAddress;
55+
}
56+
}
57+
}
Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,118 @@
1+
private async void EmailTokenItemClick(object sender, ItemClickEventArgs e)
2+
{
3+
MessageDialog md = new MessageDialog($"email address {(e.ClickedItem as SampleEmailDataType)?.EmailAddress} clicked", "Clicked Item");
4+
await md.ShowAsync();
5+
}
6+
17
private void TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
28
{
39
if (args.CheckCurrent() && args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
410
{
5-
if (string.IsNullOrWhiteSpace(sender.Text))
6-
{
7-
_ttb.SuggestedItemsSource = Array.Empty<object>();
8-
}
9-
else
11+
_acv.RefreshFilter();
12+
}
13+
}
14+
15+
private void TokenItemCreating(object sender, TokenItemAddingEventArgs e)
16+
{
17+
// Take the user's text and convert it to our data type (if we have a matching one).
18+
e.Item = this._samples.FirstOrDefault((item) => item.Text.Contains(e.TokenText, System.StringComparison.CurrentCultureIgnoreCase)) ??
19+
// Otherwise, create a new version of our data type
20+
new SampleDataType()
21+
{
22+
Text = e.TokenText,
23+
Icon = Symbol.OutlineStar
24+
};
25+
}
26+
27+
private void TokenItemAdded(TokenizingTextBox sender, object data)
28+
{
29+
if (data is SampleDataType sample)
30+
{
31+
Debug.WriteLine("Added Token: " + sample.Text);
32+
}
33+
else
34+
{
35+
Debug.WriteLine("Added Token: " + data);
36+
}
37+
}
38+
39+
private void TokenItemRemoved(TokenizingTextBox sender, TokenItemRemovingEventArgs args)
40+
{
41+
if (args.Item is SampleDataType sample)
42+
{
43+
Debug.WriteLine("Removed Token: " + sample.Text);
44+
}
45+
else
46+
{
47+
Debug.WriteLine("Removed Token: " + args.Item);
48+
}
49+
}
50+
51+
private void EmailTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
52+
{
53+
if (args.CheckCurrent() && args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
54+
{
55+
_acvEmail.RefreshFilter();
56+
}
57+
}
58+
59+
private void EmailTokenItemAdding(TokenizingTextBox sender, TokenItemAddingEventArgs args)
60+
{
61+
// Search our list for a matching person
62+
foreach (var person in _emailSamples)
63+
{
64+
if (args.TokenText.Contains(person.EmailAddress) ||
65+
args.TokenText.Contains(person.DisplayName, StringComparison.CurrentCultureIgnoreCase))
1066
{
11-
_ttb.SuggestedItemsSource = _samples.Where((item) => item.Text.Contains(sender.Text, System.StringComparison.CurrentCultureIgnoreCase)).OrderByDescending(item => item.Text);
67+
args.Item = person;
68+
return;
1269
}
1370
}
71+
72+
// Otherwise don't create a token.
73+
args.Cancel = true;
74+
}
75+
76+
private void EmailTokenItemAdded(TokenizingTextBox sender, object args)
77+
{
78+
if (args is SampleEmailDataType sample)
79+
{
80+
Debug.WriteLine("Added Email: " + sample.DisplayName);
81+
}
82+
else
83+
{
84+
Debug.WriteLine("Added Token: " + args);
85+
}
86+
87+
_acvEmail.RefreshFilter();
1488
}
1589

16-
private async void TokenItemCreating(object sender, TokenItemCreatingEventArgs e)
90+
private void EmailTokenItemRemoved(TokenizingTextBox sender, object args)
1791
{
18-
// Take the user's text and convert it to our data type.
19-
using (e.GetDeferral())
92+
if (args is SampleEmailDataType sample)
2093
{
21-
// Can do an async lookup here as well.
94+
Debug.WriteLine("Removed Email: " + sample.DisplayName);
95+
}
96+
else
97+
{
98+
Debug.WriteLine("Removed Token: " + args);
99+
}
22100

23-
e.Item = _samples.FirstOrDefault((item) => item.Text.Contains(e.TokenText, System.StringComparison.CurrentCultureIgnoreCase));
101+
_acvEmail.RefreshFilter();
102+
}
103+
104+
private void EmailList_ItemClick(object sender, ItemClickEventArgs e)
105+
{
106+
if (e.ClickedItem != null)
107+
{
108+
_ttbEmail.Items.Add(e.ClickedItem);
109+
_ttbEmail.Text = string.Empty;
110+
_acvEmail.RefreshFilter();
24111
}
112+
}
113+
114+
private void ClearButtonClick(object sender, RoutedEventArgs e)
115+
{
116+
_ttbEmail.Items.Clear();
117+
_acvEmail.RefreshFilter();
25118
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/TokenizingTextBox/TokenizingTextBoxPage.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<Grid Visibility="Collapsed">
1010
<SymbolIcon Symbol="Accept"/>
1111
<controls:TokenizingTextBox />
12+
<controls:TokenizingTextBoxItem />
1213
</Grid>
13-
</Page>
14+
</Page>

0 commit comments

Comments
 (0)