WPF:无法让我的控件获得焦点
我似乎无法让我的控件真正获得焦点:
XAML:
<Button Command={Binding SetGridToVisibleCommand} />
<Grid Visibility="{Binding IsGridVisible, Converter={con:VisibilityBooleanConverter}}">
<TextBox Text={Binding MyText} IsVisibleChanged="TextBox_IsVisibleChanged" />
</Grid>
XAML.cs:
private void TextBox_IsVisibleChanged(Object sender, DependencyPropertyChangedEventArgs e)
{
UIElement element = sender as UIElement;
if (element != null)
{
Boolean success = element.Focus(); //Always returns false and doesn't take focus.
}
}
The ViewModel does it's job of setting the IsGridVisible to true, and the converter does it's job by converting that value to
Visibility.Visible
(I snooped it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,并非所有
UIElements
都可以聚焦,您是否尝试过设置Focusable
设置为 true?Not all
UIElements
can be focused by default, have you tried settingFocusable
to true before tryingFocus()
?我们在我们的应用程序中使用它:
用法是:
最初的想法来自SO,但找不到答案。
100% 免费代码背后:P
HTH
We use this with in our application:
And the usage is:
The original idea came from SO, but couldn't find the answer.
100% free code behind :P
HTH
我无法重现您的问题,它对我来说工作得很好,在新项目上尝试以下代码作为概念证明,看看它是否适合您:
I couldn't reproduce your problem, its working fine with me, try the following code on a new project as a proof of concept and see if it works with you: