Repository navigation
Description
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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fields🆕 New
Activity
minesworld commentedon Oct 11, 2023
About the compability with existing code:
It would be nice to notice the users of TokenizingTextBox that they should check in their event handler for the case that the
TokenItemAddingEventArgsTokenTextisstring.Emptyand theItemis set instead...The solution shouldn't break existing code - but better "no surprises" for sure.
michael-hawker commentedon Oct 16, 2023
moved this to the new repository
@minesworld I'm confused,
AddTokenItemallows you to directly pass in whatever dataobjectyou want already:Windows/components/TokenizingTextBox/src/TokenizingTextBox.cs
Line 466 in 82eb9a9
And in that case, you don't need to transform it. The call to
TokenItemAddingforstringis for when the user types in something directly into the box which needs to be mapped to your programmatic model within an application.If you want to programmatically add a specific item, you can do whatever transform you need to first, and then call
AddTokenItemwith the resultantobjectof whatever type you have.Can you elaborate on your scenario and why the above isn't sufficient? Thanks!
minesworld commentedon Oct 17, 2023
Per default I'm presenting the user a List of Templates to choose from. So the choosen data should NOT be inserted. The
TokenItemAddingevent handler couldCancelthe event, but then nothing gets inserted. Of course it can call theAddTokenItemdirectly or maybe using the DispatcherQueue.Might have to check that, don't remember if I've tried
AddTokenItemwithin theTokenItemAddingevent handler - but its easier to use it this way...minesworld commentedon Oct 17, 2023
You're right. its even easier - by replacing the
Tokenin anTokenItemAddedhandler with another one using directly theTokenizingTextBoxItemsSource.Using
AddTokenItemin theTokenItemAddedevent handler would raise that event again...But : to replace an added
Tokenwith Text - setting theTokenizingTextBoxTexthas no effect within theTokenItemAddedhandler.To be able to do this this must be done by handling the
LostFocus/GotFocushandlers and doing it there. To be sure that the new AutoSuggestBox has the focus theGetIsAutoSuggestTextBoxFocused()( #259 ) method is needed... (i think)The
SuggestionListOpenis really needed - have made a pull request.enhance TokenizingTextBox by giving access to the IsSuggestionListOpe…
minesworld commentedon Oct 17, 2023
I have to revise my comment. It is a lot more code not having the
AddTokenAsyncalways fire anTokenItemAddingevent. Its near impossible to archive the same desired behaviour.I will provide within the next days (hopefully) a working example what I am doing with the
TokenizingTextBox- then you might see why this call is such usefull at least to me. But I think when others see what is possible they might to want to use it too...