如何在 WPF TabControl 中以编程方式选择 TabItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7929646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically select a TabItem in WPF TabControl
提问by Pierre Toutant
I would like to know how to select a specific TabItem in a WPF TabControl.
我想知道如何在 WPF TabControl 中选择特定的 TabItem。
I tried these bellow but nothing work!
我试过这些,但没有任何效果!
MyTabControl.SelectedIndex = x
MyTabControl.SelectedItem = MyTabItem
MyTabControl.SelectedValue = MyTabItem
MyTabItem.IsSelected = True
回答by Adrian Ratnapala
As @Chris says, any of the first three things shouldwork 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:
正如@Chris 所说,前三件事中的任何一件都应该有效,而正如@Phyxx 所说,它并不总是真正有效。问题是关于属性更改顺序的一些微妙的事情。要解决此问题,您需要让 WPF 在其自己的时间调用您的选项卡选择代码:
Dispatcher.BeginInvoke((Action)(() => MyTabControl.SelectedIndex = x));
This does just what Phyxx' timer does, but in a slightly less extreme way.
这正是 Phyxx 的计时器所做的,但以一种稍微不那么极端的方式。
回答by HCL
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
有效的
MyTabControl.SelectedIndex = x
MyTabControl.SelectedItem = MyTabItem
MyTabItem.IsSelected = True
Invalid
无效的
MyTabControl.SelectedValue = MyTabItem
回答by kishhr
Loop through the TabItems and for the tab to be selected, set
循环遍历 TabItems 并为要选择的选项卡设置
tabItem.IsSelected = true
If there are any other place due to binding changing you will see problem. Otherwise, the above code should work.
如果由于绑定更改而有任何其他地方,您将看到问题。否则,上面的代码应该可以工作。
回答by user2976441
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.
像这样不起作用的主要原因是选项卡项没有设置“名称”属性。要以编程方式导航到的选项卡控件的每个选项卡项都必须设置其 name 属性才能使上述任何代码工作。
<tabItem Name="tab1"></tabItem>
回答by Shanjee
I have implemented a small MVVM bindings based solution for selecting tab panels pragmatically.
我已经实现了一个基于 MVVM 绑定的小型解决方案,用于务实地选择选项卡面板。
define a property in your view model - Selected int type
bind the property in your view
<TabControl x:Name="TabsCandidate" VerticalAlignment="Stretch" TabStripPlacement="Top" SelectedIndex="{Binding Selected}"
private int _selected;
public int Selected { get { return _selected; } set { _selected = value; OnPropertyChanged("Selected"); } }
Set the value to Selectproperty, 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
// set the property value of the view model which points the index of the tab controller. ((CandidateViewModel)((System.Windows.FrameworkElement)candidateTab.Content).DataContext).Selected = CandidateLogTabIndex;
在您的视图模型中定义一个属性 - 选定的 int 类型
在您的视图中绑定属性
<TabControl x:Name="TabsCandidate" VerticalAlignment="Stretch" TabStripPlacement="Top" SelectedIndex="{Binding Selected}"
私人 int _selected;
public int Selected { get { return _selected; } set { _selected = value; OnPropertyChanged("Selected"); } }
将值设置为Select属性,只需绑定即可激活选项卡面板。
如果您想从父选项卡面板内的选项卡面板导航,此解决方案将很有效,您需要做的就是访问控件的数据上下文并设置它
// set the property value of the view model which points the index of the tab controller. ((CandidateViewModel)((System.Windows.FrameworkElement)candidateTab.Content).DataContext).Selected = CandidateLogTabIndex;
回答by Phyxx
I tried all the methods that shouldhave 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.
我尝试了所有应该有效的方法,但像您一样实际上没有改变所选选项卡。最后,我通过将选项卡选择代码放在DispatcherTimer
勾号中使其工作。
DispatcherTimer switchTabTimer = new DispatcherTimer();
switchTabTimer.Interval = new TimeSpan(0);
switchTabTimer.Tick += (object timerSender, EventArgs timerE) =>
{
myTabControl.SelectedIndex = 0;
switchTabTimer.Stop();
};
switchTabTimer.Start();
回答by Debasis
Try to set the MyTabControl.SelectedIndex = x
in the event handler of DataContextChanged
or Loaded
of your UI. Hope this will work.
尝试MyTabControl.SelectedIndex = x
在UI的DataContextChanged
或的事件处理程序中设置Loaded
。希望这会奏效。
回答by GreyCloud
if you don't know the index of the tab (hint its notTabIndex
) use:
如果您不知道选项卡的索引(提示不是TabIndex
),请使用:
private async Task ChangeTabTo(TabItem wantedTab) {
int index = 0;
for (var i = 0; i < TabControl.Items.Count; i++) {
var tab = TabControl.Items[i];
var t = tab as TabItem;
if (t == null) continue;
if (t == wantedTab) {
index = i;
break;
}
}
await Dispatcher.BeginInvoke((Action)(() => TabControl.SelectedIndex = index));
}
or modify it to search by name if you don't want to keep a reference to the tab
或者如果您不想保留对选项卡的引用,则将其修改为按名称搜索