如何以编程方式在 WPF TabControl 中选择 TabItem
我想知道如何在 WPF TabControl 中选择特定的 TabItem。
我尝试了以下这些但没有任何效果!
MyTabControl.SelectedIndex = x
MyTabControl.SelectedItem = MyTabItem
MyTabControl.SelectedValue = MyTabItem
MyTabItem.IsSelected = True
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
正如 @Chris 所说,前三件事中的任何一个都应该有效,而正如 @Phyxx 所说,它并不总是真正有效。问题在于属性更改顺序的一些微妙之处。要解决这个问题,您需要让 WPF 在自己的时间调用您的选项卡选择代码:
这正是 Phyxx 计时器的作用,但方式稍微不那么极端。
As @Chris says, any of the first three things should work and as @Phyxx says, it doesn't always really work. The problem is some subtle thing about the order of property changes. To work around it you need to let the WPF invoke your tab-selection code in its own time:
This does just what Phyxx' timer does, but in a slightly less extreme way.
除了第三个之外,您的所有示例都是正确的并且可以工作。问题一定出在另一个位置。也许您在设置后重置了该项目,或者您的代码从未被调用?
有效
无效
All your examples except the third one are correct and will work. The problem must be at another location. Maybe you reset the item after setting or your code never is called?
Valid
Invalid
循环遍历 TabItems 并选择要选择的选项卡,
如果由于绑定更改而有任何其他地方,您将看到问题。否则,上面的代码应该可以工作。
Loop through the TabItems and for the tab to be selected, set
If there are any other place due to binding changing you will see problem. Otherwise, the above code should work.
上面没有提到的一件事:
像这样的东西不起作用的主要原因是选项卡项没有设置“名称”属性。要以编程方式导航到的选项卡控件的每个选项卡项都必须设置其名称属性,以便上述任何代码都能工作。
One thing which hasn't been mentioned above:
The main reason something like this won't work is that the tab items do not have the "Name" property set. Each tab item of the tab control which you want to navigate to programmatically must have its name property set for any of the above code to work.
我已经实现了一个基于 MVVM 绑定的小型解决方案,用于实用地选择选项卡面板。
在视图模型中定义属性 - 选定的 int 类型
在视图中绑定属性
<前><代码>
private int _selected;
将值设置为Select属性,只需绑定即可激活选项卡面板。
如果您想从父选项卡面板内的选项卡面板进行导航,此解决方案将简单有效,您所需要做的就是访问控件的数据上下文并进行设置
I have implemented a small MVVM bindings based solution for selecting tab panels pragmatically.
define a property in your view model - Selected int type
bind the property in your view
private int _selected;
Set the value to Select property, simply the binding will activate the tab panel.
if you want to navigate from tab panel inside parent tab panels, this solution will simply works, All you need to do is, access the data context of your control and set it
尝试在 UI 的
DataContextChanged
或Loaded
的事件处理程序中设置MyTabControl.SelectedIndex = x
。希望这会起作用。Try to set the
MyTabControl.SelectedIndex = x
in the event handler ofDataContextChanged
orLoaded
of your UI. Hope this will work.我尝试了所有应该有效的方法,但就像你一样,实际上没有任何改变所选的选项卡。最后,我通过将选项卡选择代码放入
DispatcherTimer
刻度中使其工作。I tried all the methods that should have worked, but like you nothing actually changed the selected tab. In the end I got it to work by putting the tab selection code in a
DispatcherTimer
tick.如果您不知道选项卡的索引(提示其不是
TabIndex
),请使用:或者如果您不想保留引用,则将其修改为按名称搜索到选项卡
if you don't know the index of the tab (hint its not
TabIndex
) use:or modify it to search by name if you don't want to keep a reference to the tab
我在这个话题上投入了我的 2 美分,因为它可能会帮助别人。我正在使用带有 Prims 框架的 WPF。
我无法通过绑定到
SelectedItem
或SelectedIndex
来选择选项卡 - 它不起作用。我也无法从TabControl.ItemTemplate
或TabControl.ContentTemplate
中设置TabItem.Name
值。相反,我实现了基于事件的解决方案:
PubSubEvent
的类(T 是参数类型 - 在我的例子中,它是绑定到 TabItem> 的 ViewModel 对象。FindName
以编程方式设置TabControl.SelectedItem
。I'm throwing my 2 cents on the topic, since it might help someone out. I'm using WPF with Prims framework.
I was unable to select a tab by binding to
SelectedItem
orSelectedIndex
- it didn't work. I was also unable to setTabItem.Name
value from withinTabControl.ItemTemplate
orTabControl.ContentTemplate
.Instead I implemented event-based solution:
PubSubEvent<T>
(T is the type of parameter - in my case that was the ViewModel object bound to the TabItem>.TabControl.SelectedItem
programmatically usingFindName
.