简体   繁体   中英

Disable focus for control in UWP

I am building a UWP app for a RPi3 with touchsreen. I have a textbox that I give focus to on page load. I don't want to lose that focus if a user touches another control on the page, except for two specific buttons. I am using this textbox for scanner input.

I've tried disabling different properties for the controls I don't want to get focus:

AllowFocusOnInteraction="False" IsDoubleTapEnabled="False" IsHitTestVisible="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False"

But if I press any of those controls, the textbox stills loses focus.

I've also tried an event handler for textbox_LostFocus to re-give it focus, however this stops the user from clicking one the 2 buttons the user needs to click (the only controls who should receive focus) as the textbox_LostFocus event fires again to set the focus back to the textbox before the button_Click event can fire.

In a winform, I would have disabled the tabstop property. Any ideas for UWP?

Thanks in advance.

The IsEnabled property does this. If you don't like the "greyed out" look, you can change the control template.

If you want your TextBox will not lose focus, you should be able to set the focus by the Focus method in the LostFocus event.

As you may know, if we set the Focus in the LostFocus event, the Click event will not be fired.

So we should be able to add an if in your LostFocus event, when the user clicks the Button, the Text can lose focus. To do that, we can add PointerEntered event and PointerExited of the Button . In the PointerEntered event, we can set the value of setFocus .

For example:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Content="Click" AllowFocusOnInteraction="False" IsDoubleTapEnabled="False" IsHitTestVisible="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False"></Button>
        <TextBox Name="MyText" Text="Hello" LostFocus="MyText_LostFocus"></TextBox>
        <Button Name="MyButton" PointerEntered="MyButton_PointerEntered" PointerExited="MyButton_PointerExited" Click="Button_Click" Content="Submit"></Button>
    </StackPanel>
</Grid>

Code behind:

private bool setFocus = true;

private void MyText_LostFocus(object sender, RoutedEventArgs e)
{
    if (setFocus == true)
    {
        MyText.Focus(FocusState.Programmatic);
    }
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    MyButton.Focus(FocusState.Programmatic);
}

private void MyButton_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    setFocus = false;
}

private void MyButton_PointerExited(object sender, PointerRoutedEventArgs e)
{
    setFocus = true;
}

I had a similar problem to yours.

Solution in my case was setting root visual element (ScrollViewer) property AllowFocusOnInteraction to false:

var rootScrollViewer = GetVisualRootElement();
rootScrollViewer.AllowFocusOnInteraction = false;

My visual tree looks like this: ScrollViewer->Border->Frame->MainPage->StackPanel->etc...

Next step is setting AllowFocusOnInteraction to True to controls which you want to allow focus on interaction (TextBox, CheckBox etc...).

Best regards,

Adam

在您不想关注的按钮上,尝试将IsTabStop属性设置为false

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