简体   繁体   中英

how to set focus to a SearchBox control in windows 8.1 store app?

I am developing a windows 8.1 store app. I have a SearchBox control in my app. I want to set focus on it when pointer is pressed on it.

I have tried the following code but it is not working

private void SearchBox_FullView_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        SearchBox_FullView.Focus(FocusState.Pointer);
    }

How can I solve this problem?

Try this:

private void SearchBox_FullView_PointerPressed(object sender, PointerRoutedEventArgs e)

{

  SearchBox_FullView.Focus(FocusState.Programmatic);

}

My problem is solved the problem was in XAML hierarchy that was

<Grid>
<Button/> 
<SearchBox/>
<Hub/>
</Grid>

As Hub covers the whole window the Searchbox was visible but not focusable because of the Hub.

I just changed the hierarchy and put the Searchbox after the Hub and it became focusable. And by putting it after the Hub, there was no need for a button left to remove default focus from the SearchBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM