CommunityToolkit / CommunityToolkit/Windows

Make TokenizingTextBox more flexible

Aperta
#256 6 commenti 1 reazione 0 assegnatari Vedi su GitHub
components::controls::tokenizingtextbox enhancement need more info :pushpin: question
Lingua principale
C#
Stelle
1.1k
Fork
166
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### 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

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start by reading TokenizingTextBox.cs and TokenizingTextBoxItem.AutoSuggestBox.cs, focusing on AddTokenAsync, TokenItemAdding, and the AutoSuggestBox wrapper. Done means non-string data can reach TokenItemAdding and the TokenizingTextBox exposes IsSuggestionListOpen while preserving existing behavior; verify the affected control behavior with the project's available checks.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
frontend
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.