CommunityToolkit / CommunityToolkit/Windows

Make TokenizingTextBox more flexible

Offen
#256 6 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
components::controls::tokenizingtextbox enhancement need more info :pushpin: question
Vorherrschende Sprache
C#
Sterne
1.1k
Forks
166
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

### Describe the problem

The current model only allows a single use case: adding text as `AddTokenAsync()` `data ` has to be a `string`. Otherwise the `TokenItemAdding` event isn't raised and there is no chance to change the `Item `to be added.

By **always raising** the `TokenItemAdding `event the TokenizingTextBox could be used in ways not possible without.

Also needed: having the possibility to access the `AutoSuggestBox ` `IsSuggestionListOpen` property...

### Describe the solution

**TokenizingTextBox.cs**

if data is not a string, call the event with `string.Empty` and the item set to data... should be compatible this way with existing code.

```
internal async Task AddTokenAsync(object data, bool? atEnd = null)
{
if (ReadLocalValue(MaximumTokensProperty) != DependencyProperty.UnsetValue && (MaximumTokens <= 0 || MaximumTokens <= _innerItemsSource.ItemsSource.Count))
{
// No tokens for you
return;
}

if (TokenItemAdding != null)
{
TokenItemAddingEventArgs tiaea;
if (data is string str)
{
tiaea = new TokenItemAddingEventArgs(str);
}
else
{
tiaea = new TokenItemAddingEventArgs(string.Empty);
tiaea.Item = data;
}

await TokenItemAdding.InvokeAsync(this, tiaea);

if (tiaea.Cancel)
{
return;
}

if (tiaea.Item != null)
{
data = tiaea.Item; // Transformed by event implementor
}
}

// If we've been typing in the last box, just add this to the end of our collection
if (atEnd == true || _currentTextEdit == _lastTextEdit)
{
_innerItemsSource.InsertAt(_innerItemsSource.Count - 1, data);
}
else
{
// Otherwise, we'll insert before our current box
var edit = _currentTextEdit;
var index = _innerItemsSource.IndexOf(edit);

// Insert our new data item at the location of our textbox
_innerItemsSource.InsertAt(index, data);

// Remove our textbox
_innerItemsSource.Remove(edit);
}

// Focus back to our end box as Outlook does.
var last = ContainerFromItem(_lastTextEdit) as TokenizingTextBoxItem;
last?._autoSuggestTextBox.Focus(FocusState.Keyboard);

TokenItemAdded?.Invoke(this, data);

GuardAgainstPlaceholderTextLayoutIssue();
}
```

```
public bool IsSuggestionListOpen
{
get => (ContainerFromItem(_lastTextEdit) is TokenizingTextBoxItem lastContainer) ? lastContainer.IsSuggestionListOpen : false;
set
{
if (ContainerFromItem(_lastTextEdit) is TokenizingTextBoxItem lastContainer) lastContainer.IsSuggestionListOpen = value;
}
}
```

**TokenizingTextBoxItem.AutoSuggestBox.cs**

```
internal bool IsSuggestionListOpen
{
get => (_autoSuggestBox != null) ? _autoSuggestBox.IsSuggestionListOpen : false;
set { if (_autoSuggestBox != null) _autoSuggestBox.IsSuggestionListOpen = value; }
}
```

### Alternatives

None

### Additional info

Sorry for not providing a pull request.

Spent hours integrating the needed source files into my project in Visual Studio... . "total mess" for someone new to Windows development just wanting to code and having no clue about the tool chain.

### Help us help you

None

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit dem Lesen von TokenizingTextBox.cs und TokenizingTextBoxItem.AutoSuggestBox.cs und konzentriere dich dabei auf AddTokenAsync, TokenItemAdding und den AutoSuggestBox-Wrapper. Als erledigt gilt die Aufgabe, wenn Daten, die keine Strings sind, TokenItemAdding erreichen können und TokenizingTextBox IsSuggestionListOpen bereitstellt, wobei das bestehende Verhalten erhalten bleibt; überprüfe das Verhalten des betroffenen Controls mit den im Projekt verfügbaren Prüfungen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
frontend
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.