Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know howto generate TabItems on the fly (runtime) but I need a way to check if the tabItem exists before it is created.


C#
public void NewTabItem(object sender, RoutedEventArgs e)
{
  if( .........)
  {
    this.MyTabControl.SelectedIndex =
      this.MyTabControl.Items.Add(new TabItem { Header = "A1"});
  }
}
Posted

check the values in the MyTabControl if the header already exists or some data in the items.
 
Share this answer
 
This question is really old but still... it is nice to have an answer

C#
        private bool createTab( List<string> tabNames, NIC savedSetting )
        {
            string newTabeName = savedSetting.AdapterName;
            bool tabExists = false;

            foreach(var checkTab in tabSettings.Items)
            {
                //cast as a TabItem to get access to the properties
                if( ( ( TabItem )checkTab ).Header.ToString() == newTabeName )
                {
                    tabExists = true;
                    break;
                }
            }

            if( !tabExists )
            {
                var NIC_Tab = new TabItem()
                {                    
                    Header = newTabeName,
                    Content = new TabDataControl(),
                    DataContext = savedSetting
                };
                tabSettings.Items.Add( NIC_Tab );
            }
            return tabExists;
        }
</string>
 
Share this answer
 
C#
private bool createTab( List<string> tabNames, NIC savedSetting )
        {
            string newTabeName = savedSetting.AdapterName;
            bool tabExists = false;

<pre>
        foreach(var checkTab in tabSettings.Items)
        {
            //cast as a TabItem to get access to the properties
            if( ( ( TabItem )checkTab ).Header.ToString() == newTabeName )
            {
                tabExists = true;
                break;
            }
        }

        if( !tabExists )
        {
            var NIC_Tab = new TabItem()
            {
                Header = newTabeName,
                Content = new TabDataControl(),
                DataContext = savedSetting
            };
            tabSettings.Items.Add( NIC_Tab );
        }
        return tabExists;
    }
 
Share this answer
 
Comments
CHill60 8-Jul-15 9:40am    
Reposting someone else's solution will not win you any friends

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900