所以我的情况是我有一个TabControl,网格内有两个标签。一个按钮也在网格中,因此它出现在两个选项卡上。单击此按钮时,我希望它能够打开Tab1,执行X,当Tab2打开时,执行Y.我知道我可以在每个选项卡中创建一个按钮并且很好,但我想知道是否有办法不这样做。
示例:
<Grid>
<TabControl x:Name="sampTabControl">
<TabItem x:Name="Tab1" Header="This Is Tab1"/>
<TabItem x:Name="Tab2" Header="This Is Tab2"/>
</TabControl>
<Button Width="50" Height="50" Name="sampBtn" Click="doSomething_Click/>
</Grid>
private void doSomething_Click(object sender, System.Windows.RoutedEventArgs e)
{
if(Tab1.IsOpen //IsOpen is something I made up, does not actually work
{
//Perform X Code
}
else if(Tab2.IsOpen) //IsOpen is something I made up, does not actually work
{
//Perform Y Code
}
}
答案 0 :(得分:3)
如何使用sampTabControl.SelectedIndex
属性?