Added support for TokenSelectionMode in TokenizingTextBox

This commit is contained in:
Shane Weaver 2021-08-04 15:19:31 -07:00
Родитель 794b295bb9
Коммит fafbf5b335
4 изменённых файлов: 75 добавлений и 1 удалений

Просмотреть файл

@ -39,7 +39,8 @@
MaxHeight="104"
HorizontalAlignment="Stretch"
TextMemberPath="Text"
TokenDelimiter=",">
TokenDelimiter=","
TokenSelectionMode="Single">
<controls:TokenizingTextBox.SuggestedItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
@ -75,6 +76,7 @@
QueryIcon="{ui:SymbolIconSource Symbol=Find}"
TextMemberPath="Text"
TokenDelimiter=","
TokenSelectionMode="Multiple"
IsItemClickEnabled="True"
TokenItemTemplate="{StaticResource EmailTokenTemplate}">
</controls:TokenizingTextBox>

Просмотреть файл

@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// Indicates how tokens are selected in the <see cref="TokenizingTextBox"/>.
/// </summary>
public enum TokenSelectionMode
{
/// <summary>
/// Only one token can be selected at a time. A new token should replace the active selection.
/// </summary>
Single,
/// <summary>
/// Multiple tokens can be selected at a time.
/// </summary>
Multiple,
}
}

Просмотреть файл

@ -157,6 +157,26 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
typeof(TokenizingTextBox),
new PropertyMetadata(false));
/// <summary>
/// Identifies the <see cref="TokenSelectionMode"/> property.
/// </summary>
public static readonly DependencyProperty TokenSelectionModeProperty = DependencyProperty.Register(
nameof(TokenSelectionMode),
typeof(TokenSelectionMode),
typeof(TokenizingTextBox),
new PropertyMetadata(TokenSelectionMode.Multiple, OnTokenSelectionModeChanged));
private static void OnTokenSelectionModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TokenizingTextBox ttb && e.NewValue is TokenSelectionMode newTokenSelectionMode && newTokenSelectionMode == TokenSelectionMode.Single)
{
while (ttb.Items.Count > 1)
{
ttb.Items.RemoveAt(ttb.Items.Count - 1);
}
}
}
/// <summary>
/// Gets or sets the Style for the contained AutoSuggestBox template part.
/// </summary>
@ -303,5 +323,16 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
return PrepareSelectionForClipboard();
}
}
/// <summary>
/// Gets or sets how the control should display tokens.
/// <see cref="TokenSelectionMode.Multiple"/> is the default. Multiple tokens can be selected at a time.
/// <see cref="TokenSelectionMode.Single"/> indicates that only one token can be present in the control at a time.
/// </summary>
public TokenSelectionMode TokenSelectionMode
{
get => (TokenSelectionMode)GetValue(TokenSelectionModeProperty);
set => SetValue(TokenSelectionModeProperty, value);
}
}
}

Просмотреть файл

@ -448,6 +448,19 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
}
}
if (TokenSelectionMode == TokenSelectionMode.Single)
{
// Remove any existing tokens.
for (var i = _innerItemsSource.Count - 1; i >= 0; --i)
{
var item = _innerItemsSource[i];
if (item is not ITokenStringContainer)
{
_innerItemsSource.Remove(item);
}
}
}
// If we've been typing in the last box, just add this to the end of our collection
if (atEnd == true || _currentTextEdit == _lastTextEdit)
{