Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent TextBlock from getting automatic focus

I have the following XAML excerpt :

      <SplitView Name="Menu" DisplayMode="CompactOverlay" OpenPaneLength="200" CompactPaneLength="0" Grid.RowSpan="2">
            <SplitView.Pane>
                <StackPanel>
                    <AutoSuggestBox Margin="0,20,0,20" Width="170" PlaceholderText="Search" QueryIcon="Find"></AutoSuggestBox>
                    <ListBox>
                        <ListBoxItem Tapped="Projects_Tapped">
                            <StackPanel Orientation="Horizontal">
                                <SymbolIcon Symbol="Library" />
                                <TextBlock Margin="10,0,0,0">Projects</TextBlock>
                            </StackPanel>
                        </ListBoxItem>
                        [....]
                    </ListBox>
                </StackPanel>
            </SplitView.Pane>
        </SplitView>

Basically, this splitview is compacted until the user presses a button which then sets the IsPaneOpen to true, which in turn shows my application menu.

The problem is, the very first thing i have in the menu is the search box and it seems to be getting focused automatically no matter what i do. The fact that it has focus then brings up the touch keyboard on phones, which is very annoying and hides most of the menu on small phones.

I tried playing with the TabIndex property to either give it a huge number or even put a lower index for something else.

I also tried setting the IsTabStop to false but that didn't seem to do anything.

Is there a clean way to prevent the box from gaining focus automatically? (Besides disabling / hiding the element and then enabling / showing it again)

like image 566
Dany Khalife Avatar asked Nov 26 '22 04:11

Dany Khalife


1 Answers

You can try the following:

Give ListBox a name:

<ListBox Name="MyListBox">

Then, where you call IsPaneOpen to true, add the following line:

Menu.IsPaneOpen = true;
MyListbox.Focus(FocusState.Programmatic);

This would change the focus to the ListBox element and dismiss the keyboard when you open the SplitView.

like image 118
Justin Lam Avatar answered Feb 23 '23 06:02

Justin Lam