wpf 根据字符串值在选项卡控件中搜索特定选项卡项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18275145/
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
Search through Tab Control for specific Tab Item based on a String value
提问by Eric after dark
I would like to know how to select a tabItemin a tabControlwhose name matches a specific string value. I'm guessing that I will have to do some sort of search.
我想知道如何tabItem在tabControl名称与特定字符串值匹配的 a 中选择一个。我猜我将不得不进行某种搜索。
Here's a visual example:
这是一个视觉示例:
string selectedTabItem = "TabItem";
//if there exists a Tab Item in this specific tab control
//with the above string as it's Name
//that Tab Item .IsSelected = true;
回答by dkozl
Assuming that you create your tabs manually, and not via bindings, then this should work:
假设您手动创建选项卡,而不是通过绑定,那么这应该有效:
tabControl.SelectedItem = tabControl.Items.OfType<TabItem>().SingleOrDefault(n => n.Name == selectedTabItem);
回答by Bojan
foreach (TabPage t in myTabControl.TabPages)
{
if t.Name.Equals("something")
{
myTabControl.SelectedTab = t;
break;
}
}
Basically you can loop through each tab and and see if the name matches your string
基本上,您可以遍历每个选项卡并查看名称是否与您的字符串匹配
a better way to do it in my opinion is:
在我看来,更好的方法是:
if (myTabControl.TabPages.ContainsKey("something"))
myTabControl.SelectedTab = mytabControl.TabPages["something"];

