在 WPF 中以编程方式选择 tabItem
我在 TabControl 中有不同的 tabItems 每个 tabItem 都有一些输入字段。
我以编程方式在 tabItems 之间移动(就像从第一个移动到下一个的向导)
我在“下一步”按钮中使用此代码
tabItem2.isSelected = true;
我的问题是,当我通过单击选项卡项目在选项卡项目之间移动时,焦点(键盘焦点)将移动到第一个文本框输入。
但以编程方式使用前面的代码,焦点不会移动到 tabItem 内的第一个输入文本框项。
任何想法?
I have different tabItems in a TabControl
and each tabItem has some input fields.
I am moving between the tabItems programmatically (like a wizard to move from the first to the next)
I am using this code inside the "Next" button
tabItem2.isSelected = true;
my problem that when I move between the tabItems by clicking on them, the focus (keyboard focus) will move to the first textbox input.
But programmatically with the previous code, the focus won't move to the first input textbox item inside the tabItem.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您强制使用 IsSelected 属性,我还会为第一个 TextBox 指定一个名称,并在设置所选选项卡后设置焦点。
如果您动态构建 UI,这将不起作用,但您可以创建一个实用程序方法,该方法在逻辑树(或视觉树,如果您使用演示者/视图模型)中搜索第一个输入控件,然后设定焦点。
If you're forcing the IsSelected property, I'd also give the first TextBox a name and set the focus after you set the selected tab.
If you're building your UI dynamically, this won't work, but you can create a utility method which searches the logical tree (or the visual tree if you're using presenters/view-models) for the first input control and then set the focus.
这些解决方案对我不起作用。 它已经选择了我想要的 TabItem,但无法选择/聚焦所需的 TreeViewItem。 (如果已经选择了 TabItem,它只会聚焦 TVI。)下面的解决方案最终对我有用。
(仅供参考:下面的代码片段是类似于 Microsoft Help Viewer 2.0 的应用程序的一部分。当您单击“同步”按钮时,它首先选择“内容”选项卡(如果尚未选择),然后遍历树视图,直到找到匹配的树然后它会选择/聚焦查看项目。)
干杯
These solutions didn't work for me. It got as far selecting the TabItem I wanted, but it wasn't able to select/focus the desired TreeViewItem. (It would only focus the TVI if the TabItem was already selected.) The solution below finally worked for me.
(FYI: The snippets below are part of app that is similar to Microsoft Help Viewer 2.0. When you click the "Sync" button, it first selects the Contents tab if not already selected, then traverses into tree view until it finds the matching tree view item. Which it then selects/focuses.)
Cheers