当按下或单击回车键时,文本框失去焦点
我在画布上有一个文本框。当我按 Enter 键和/或单击画布时,如何失去焦点(以便插入符号消失)?我的文本框位于按钮模板中。
我已经尝试了几乎所有的方法,但它不起作用:
FocusManager.SetIsFocusScope(mainCanvas, true)
mainCanvas.Focus();
FocusManager.SetFocusedElement(child, parent);
有趣的是,如果我使用按钮,它可以与 KeyBoard.Focus(button) 一起使用,但它不能与画布一起使用,有谁知道为什么或有任何其他建议?
I have a textbox on a canvas. How do I lose the focus (so the caret goes away) when I press enter and/or click on the canvas? My textbox is in a template for a button.
I've tried pretty much everything and it doesn't work:
FocusManager.SetIsFocusScope(mainCanvas, true)
mainCanvas.Focus();
FocusManager.SetFocusedElement(child, parent);
it is interesting that if I use a button it works with KeyBoard.Focus(button) but it doesn't work with a canvas, does anyone know why or have any other suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Canvas 的问题在于,当您单击它时,除非背景不是白色,否则实际上不会发生单击事件。
如果您想要白色,一个技巧是使用白色 -1 或 #FFFFFE,或者如果父对象也是白色,则使用透明。所以没人能看出它不是白色的。
现在您的点击事件可以发生。
您还需要使其可聚焦。
MainWindow.xaml
MainWindow.xaml.cs
如何使 WPF Canvas 鼠标单击事件起作用?
我在我的博客上发表了一篇更完整的文章。
The problem with Canvas is that when you click on it, you don't actually get the click event to occur unless you have a background that is not white.
One trick if you want white is to use white -1 or #FFFFFE or if the parent is also white use Transparent. So no one can tell it isn't white.
Now your click event can occur.
Also you need to make it focusable.
MainWindow.xaml
MainWindow.xaml.cs
How to make the WPF Canvas mouse click event work?
I made a more complete post on my blog.