CommunityToolkit / CommunityToolkit/Windows

Make TokenizingTextBox more flexible

オープン
#256 コメント 6 件 リアクション 1 件 担当者 0 名 GitHub で見る
components::controls::tokenizingtextbox enhancement need more info :pushpin: question
主要言語
C#
スター
1.1k
フォーク
166
PR マージ指標
30日以内にマージされた PR はありません

説明

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

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

まず TokenizingTextBox.cs と TokenizingTextBoxItem.AutoSuggestBox.cs を読み、AddTokenAsync、TokenItemAdding、AutoSuggestBox wrapper に注目します。既存の動作を維持したまま、文字列以外のデータが TokenItemAdding に到達でき、TokenizingTextBox が IsSuggestionListOpen を公開することを完了条件とします。プロジェクトで利用可能なチェックを使って、影響を受けるコントロールの動作を確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
csharp
領域
frontend
issue の種類
機能追加
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。