当选择更改时,如何将焦点设置到 WPF 中的数据绑定文本框?
我有一个与 TreeView 数据绑定的文本框。如果所选元素发生变化,文本框会显示它的名称。我想要的是,如果选择已更改,则将焦点设置到文本框并选择文本框中的文本。
这只能通过 WPF 实现吗?何时、如何实现?
(树视图“OnSelectionChange”上的事件没有问题,但这不是问题;-))
<TextBox Name="textBoxTitel" DataContext="{Binding ElementName=treeView, Path=SelectedItem}" />
I have a TextBox that is databound to a TreeView. If the selected element chnages the TextBox shows me the Name of it. What i want is, set the focus to the TextBox if the selection has changed and select the text in the TextBox.
Is this possible only with WPF and when, how?
(it is no problem with a event on the treeview "OnSelectionChange" but that's not the question ;-) )
<TextBox Name="textBoxTitel" DataContext="{Binding ElementName=treeView, Path=SelectedItem}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅使用内置工具无法在 XAML 中完成此操作。最后,您将需要编写代码,并且源文件的命名方式实际上没有任何区别。但我将勾画出一个不需要代码隐藏文件中的代码的解决方案。
从 TriggerAction< 派生您自己的类
MyAction
/a>.添加MyAction
作为Treeview.SelectedItemChanged
为树视图的样式。通过绑定到您将在MyAction
上定义的合适的依赖属性,指示它设置焦点并选择文本框的文本,例如,至少代码将是可重用的。
It cannot be done in XAML using only built-in facilities. In the end you are going to need to write code, and it really doesn't make any difference how the source file is named. But I 'll sketch out a solution that will not require code in your code-behind file.
Derive your own class
MyAction
from TriggerAction. Add aMyAction
as an event trigger forTreeview.SelectedItemChanged
to the style of your treeview. By binding to suitable dependency properties that you will define onMyAction
, instruct it to set focus and select the text of your text box, e.g.At least the code will be reusable.