Remove focus on first textbox

会有一股神秘感。 提交于 2020-01-01 09:08:25

问题


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?


回答1:


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>



回答2:


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:


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;


来源:https://stackoverflow.com/questions/18121089/remove-focus-on-first-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!