WPF TabControl - TabItem [英] WPF TabControl - TabItem

查看:160
本文介绍了WPF TabControl - TabItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何动态生成TabItems(运行时)但是我需要一种方法来检查tabItem在创建之前是否存在。





I know howto generate TabItems on the fly (runtime) but I need a way to check if the tabItem exists before it is created.


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

推荐答案

检查MyTabControl中的值,如果标头已经存在或者某些数据在items。
check the values in the MyTabControl if the header already exists or some data in the items.


这个问题真的很旧但是仍然......很高兴有答案



This question is really old but still... it is nice to have an answer

        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>


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;
    }


这篇关于WPF TabControl - TabItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆