-api-id | -api-type |
---|---|
M:Microsoft.UI.Input.InputFocusController.TrySetFocus |
winrt method |
Attempts to set focus to the ContentIsland associated with the InputFocusController.
True, if focus was set successfully; otherwise, false.
Due to other message processing requirements, focus might move from the ContentIsland by the time this request is processed.
A user might also move focus before this request is processed.
Processing this request can raise GotFocus and LostFocus events in quick succession.
The following example shows how to indicate focus is on a TextBox within a ContentIsland based on pointer input.
void OnClick(PointerPoint clickLocation)
{
if (IsWithinBoundsOfTextBox(clickLocation))
{
InputFocusController focusController = InputFocusController.GetForIsland(myIsland);
if (!focusController.HasFocus())
{
bool nowHasFocus = focusController.TrySetFocus();
// Change styling of text box based on whether the Island received focus
if (nowHasFocus)
{
DrawTextBoxBorder();
}
else
{
RemoveTextBoxBorder();
}
}
}
}