2017-02-09 81 views
1

我正在用触摸屏为RPi3构建一个UWP应用程序。我有一个文本框,我将重点放在页面加载上。如果用户触摸页面上的另一个控件(除了两个特定按钮),我不想失去焦点。我正在使用此文本框进行扫描仪输入。在UWP中禁用焦点控制

我试图禁用,因为我不想获得焦点的控件不同的属性:

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

但是,如果我按任何这些控件,文本框剧照失去焦点。

我也尝试过一个textbox_LostFocus的事件处理程序重新给它的焦点,但是这会阻止用户单击一个用户需要单击的2个按钮(唯一控制谁应该接收焦点)作为textbox_LostFocus事件在button_Click事件触发前再次触发焦点回到文本框。

在一个winform中,我会禁用tabstop属性。 UWP的任何想法?

在此先感谢。

回答

1

如果你希望你的文本框不会失去焦点,你应该能够通过Focus方法在LostFocus设置对焦事件。

如您所知,如果我们在LostFocus事件中设置Focus,则无法触发Click事件。

因此,我们应该可以在您的LostFocus事件中添加if,当用户单击按钮时,文本可能会失去焦点。为此,我们可以添加PointerEntered事件和ButtonPointerExited。在PointerEntered事件中,我们可以设置值setFocus

例如:

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

后面的代码:

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; 
} 
0

上的按钮,你不想注重,尝试IsTabStop属性设置为false

+0

(此属性也是UWP) – PrisonMike

+1

[IsTabStop](https://docs.microsoft.com/en-us/uwp/api/Windows.UI。 Xaml.Controls.Control#Windows_UI_Xaml_Controls_Control_IsTabStop)*“表示控件是否包含在标签导航中。”*换句话说,它控制**键盘导航。这不是问题的要求。 – IInspectable

+0

哦,我以为他的意思是键盘焦点,因为如果他们没有焦点是不可能按下按钮,他还说,在winforms中,他会使用isTabStop属性,所以我只是指出这也是可用的在uwp – PrisonMike

1

IsEnabled property做到这一点。如果你不喜欢“变灰”的外观,你可以改变控制模板。

1

我和你有类似的问题。

解决方案在我的情况被设定根视觉元素(ScrollViewer中)财产AllowFocusOnInteraction为false:

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

我的视觉树是这个样子:ScrollViewer-> Border->帧 - > MainPage-> StackPanel-> etc ...

下一步是将AllowFocusOnInteraction设置为True来控制你想要允许关注交互(TextBox,CheckBox等等)。

此致

亚当