Remove focus on first textbox

2019-04-06 16:35发布

In our Windows Store app we have a textbox, and when the application starts this textbox always get focus. In a desktop scenario that's no problem, but on a tablet device this focus will directly open the onscreen keyboard which is not a scenario that we want.

We tried to set the focus on another control programmatic with the .Focus(FocusState) method, but somehow the focus is set back to the textbox. We have both set the focus in the LoadState or the OnNavigatedTo method.

Only when we have timer we have set the focus succesfully to another control. Anyone has ideas how to set the focus to another control, or preferably set not focus at all to an control?

3条回答
我命由我不由天
2楼-- · 2019-04-06 17:03

IsTabStop="true" didn't work for me. My solution is to call UpdateLayout() of the scrollViewer before setting the focus on the TextBox:

scrollViewer.UpdateLayout();

textBox.Focus(Windows.UI.Xaml.FocusState.Programmatic);

查看更多
爷、活的狠高调
3楼-- · 2019-04-06 17:05

If you don't like accepted answer with the ScrollViewer you can also do this to remove the focus:

_textBox.IsReadOnly = true;
_textBox.IsReadOnly = false;
查看更多
欢心
4楼-- · 2019-04-06 17:20

Normally you can set the Focus on any element by TextBox.Focus(). However I discovered the same behaviors (autofocus on start) when you place your TextBox inside a ScrollViewer or a FlyOut. Then you have to set the IsTabStop on the parent-element:

<ScrollViewer IsTabStop="true">
   <TextBox />
</ScrollViewer>
查看更多
登录 后发表回答