如何在 WPF TabControl 中以编程方式选择 TabItem [英] How to programmatically select a TabItem in WPF TabControl

查看:41
本文介绍了如何在 WPF TabControl 中以编程方式选择 TabItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 WPF TabControl 中选择特定的 TabItem.

I would like to know how to select a specific TabItem in a WPF TabControl.

我尝试了以下这些,但没有任何效果!

I tried these bellow but nothing work!

MyTabControl.SelectedIndex = x

MyTabControl.SelectedItem = MyTabItem

MyTabControl.SelectedValue = MyTabItem

MyTabItem.IsSelected = True

推荐答案

正如@Chris 所说,前三项中的任何一项应该起作用,而正如@Phyxx 所说,它并不总是真正起作用.问题是关于属性更改顺序的一些微妙的事情.要解决此问题,您需要让 WPF 在其自己的时间调用您的选项卡选择代码:

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:

Dispatcher.BeginInvoke((Action)(() => MyTabControl.SelectedIndex = x));

这与 Phyxx 的计时器所做的一样,但方式稍微不那么极端.

This does just what Phyxx' timer does, but in a slightly less extreme way.

这篇关于如何在 WPF TabControl 中以编程方式选择 TabItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆