单击标签以聚焦 WPF 中的另一个控件
我已经离开 WPF 大约一年了,我被这个简单的问题难住了。我发誓有一种简单的方法可以告诉标签在单击时将焦点集中到另一个控件。
<StackPanel>
<Label Target="TextBox1">Label Text</Label>
<TextBox Name="TextBox1" />
</StackPanel>
当用户单击“标签文本”时,我希望文本框接收焦点。这可能吗?
I have taken a break from WPF for about a year and I am stumped by this simple problem. I swear there was an easy way to tell a label to focus to another control when it is clicked.
<StackPanel>
<Label Target="TextBox1">Label Text</Label>
<TextBox Name="TextBox1" />
</StackPanel>
When the user clicks on "Label Text" I want the TextBox to receive focus. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该利用 Target 属性:
首先使用 Label 而不是 TextBlock 的全部目的是利用其关联功能,请参阅 MSDN 上的参考。
关于我的笔记,我问了一个有关如何获得真正点击的问题 这里,如果你好奇的话。
You should make use of the Target property:
The whole point of using a Label in the first place instead of a TextBlock is to make use of its associative functionality, see the reference on MSDN.
About my note, i asked a question about how to get a real click over here, if you are curious.
我找到了我用于此目的的代码,并认为我会分享它,以防它对其他人有用。
I found the code I used to use for this and figured I would share it in case it is useful for anyone else.
你不能用快捷键组合来做到这一点吗
can't you do that with the shortcut key combination
根据阅读HTML“for”属性的 WPF 标签对应部分,您需要一个附加的行为来做到这一点。
Based on reading WPF label counterpart for HTML "for" attribute, you'd need an attached behavior to do that.