How to prevent TextBlock from automatically focusing

I have the following excerpt from XAML:

<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 clicks a button, which then sets IsPaneOpen to true, which in turn displays my application.

The problem is that the first thing I have on the menu is the search box, and it seems to automatically focus regardless of what I do. The fact that he has focus then calls up the touch keyboard on the phones, which is very annoying and hides most of the menu on small phones.

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

I also tried setting IsTabStop to false, but did nothing.

Is there a clean way to prevent auto focusing? (In addition to disabling / hiding the item and then enabling / re-displaying)

+5
source share
3 answers

You can try the following:

Give ListBox name:

 <ListBox Name="MyListBox"> 

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

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

This will change focus to the ListBox and release the keyboard when you open SplitView .

+1
source

This TextBox field inside the AutoSuggestBox automatically extracts focus.

To fix the problem, you can edit the AutoSuggestBox template with:

Click “Document Outline Tag” → select “AutoSuggestBox Element” from the document tree → right-click-> Change Template-> Change Copy.

Then VS will add the template to the Resources page. Add IsTabStop="False" to the TextBox inside the template, as shown below:

 <ControlTemplate TargetType="AutoSuggestBox"> <Grid> <VisualStateManager.VisualStateGroups> ... </VisualStateManager.VisualStateGroups> <TextBox x:Name="TextBox" IsTabStop="False" ScrollViewer.BringIntoViewOnFocusChange="False" DesiredCandidateWindowAlignment="BottomEdge" Header="{TemplateBinding Header}" Margin="0" PlaceholderText="{TemplateBinding PlaceholderText}" Style="{TemplateBinding TextBoxStyle}" Width="{TemplateBinding Width}" Canvas.ZIndex="0"/> <Popup x:Name="SuggestionsPopup"> ... </Popup> </Grid> </ControlTemplate> 

Then it will automatically stop focusing.

0
source

I just installed TabIndex on 999 and this solved my problem:

 <AutoSuggestBox TabIndex="999" /> 

Before I tried to accept the answer, but it does not cause focus at all, i.e. it is generally impossible to use a text field (cannot focus on it, the keyboard does not appear, etc.).

-1
source

Source: https://habr.com/ru/post/1247902/


All Articles