【问题标题】:Find Active/Open Tab In WPF在 WPF 中查找活动/打开选项卡
【发布时间】:2016-06-09 01:14:43
【问题描述】:

所以我的情况是我有一个 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
           }
    }

【问题讨论】:

    标签: wpf tabcontrol tabitem


    【解决方案1】:

    使用sampTabControl.SelectedIndex 属性怎么样?

    【讨论】:

    • 附加说明,对于像我这样愚蠢的人,在我的示例中,Tab1 的 SelectedIndex 为 0,Tab2 的 SelectedIndex 为 1。
    猜你喜欢
    • 2013-10-27
    • 2012-01-04
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多