WPF Tab Control: How do I get the currently selected tab?

走远了吗. 提交于 2019-12-12 09:29:14

问题


In my tab SelectionChanged event (is this the correct event, I can't find a tab changed event?), how do I access the new tab?

Also from outside this event in normal code, how do I access the currently selected tab?

  TabControl.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(TabControl_SelectionChanged);

  void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  {
      //How so access my currently selected tab???
  }

回答1:


TabControl.SelectedItem is the selected tab.

cast it to a TabItem to get the properties.

What I mostly do is bind it to a viewmodel.




回答2:


you can use the TabControl.SelectedItem property, it will get you the selected TabItem

Sample shown below

TabItem ti = Tabs1.SelectedItem as TabItem;
MessageBox.Show("This is " + ti.Header + " tab");



回答3:


TabControl.SelectedIndex gets you the currently selected tab index in version 3.0 and later of the .NET Framework.



来源:https://stackoverflow.com/questions/4255017/wpf-tab-control-how-do-i-get-the-currently-selected-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!