Skip to content

Latest commit

 

History

History
57 lines (41 loc) · 1.56 KB

inputfocuscontroller_trysetfocus_432736152.md

File metadata and controls

57 lines (41 loc) · 1.56 KB
-api-id -api-type
M:Microsoft.UI.Input.InputFocusController.TrySetFocus
winrt method

Microsoft.UI.Input.InputFocusController.TrySetFocus

-description

Attempts to set focus to the ContentIsland associated with the InputFocusController.

-returns

True, if focus was set successfully; otherwise, false.

-remarks

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.

-see-also

-examples

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();
            }
        }
    }
}