Slepz
Slepz

Reputation: 460

How do I set the location of the the text cursor in an AutoSuggestBox?

I'm have an AutoSuggestBox that need to capitalize input while the user types it. The problem is that when I set the text to caps, the cursor is moved to the end of the end of the text. I need to determine the location of the cursor before I change the text and set the selection after.

In Android I would use the SelectionStart and SetSelection functions for this, but AutoSuggestBox doesn't seem to have anything like that. TextBox has a SelectionStart and SelectionLength properties and a Select function. Perhaps AutoSuggestBox has a TextBox for a child that I need to get access to somehow?

Upvotes: 2

Views: 332

Answers (1)

David Grochocki
David Grochocki

Reputation: 670

Many TextBox properties and methods are not exposed through AutoSuggestBox. You can grab the TextBox inside the control template with something like:

TextBox textBox = this.AutoSuggestBox.GetDescendants<TextBox>().FirstOrDefault();

GetDescendants(), in this case, is just a helper function that takes advantage of VisualTreeHelper to crawl the tree looking for the specified type (abstracted away for simplicity).

Ideally, you would do this in OnApplyTemplate(), but since AutoSuggestBox is sealed, you cannot override this method. Depending on how you structure your UI and when you need access to the TextBox, you may need to explicitly make a call to UpdateLayout() to ensure the TextBox is available via GetDescendants() with:

this.AutoSuggestBox.UpdateLayout();

Upvotes: 3

Related Questions