Search code examples
wpfwindows-8.1search-box

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?


Solution

  • 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.