将数据绑定到Windows窗体TabControl

时间:2012-08-30 10:30:16

标签: c# winforms tabcontrol

我正在尝试我的第一个Windows Form项目,以前完全基于Web,遇到了一些问题。我想将一个对象列表绑定到TabControl并创建Tabs,然后从每个选项卡的click事件中访问数据绑定值。

我想要绑定的对象是

public class TreeNodeItem
{
    private NTree<string> node;

    public TreeNodeItem(NTree<string> node)
    {
        this.node = node;
    }

    public string Value
    {
        get { return this.node.data; }
    }
}

NTree节点表示对象中的一个节点,该节点对树结构中的数据进行建模。我想为列表中的每个对象创建一个选项卡,其Value属性绑定到Tab Text属性。其他帖子提到绑定到控件的ItemsSource属性,但Visual Studio没有给我这个属性。

非常感谢任何帮助。

干杯

斯图尔特

1 个答案:

答案 0 :(得分:2)

好的,我没有意识到绑定是必须的。虽然我从未在Windows窗体应用程序中看到过这样的事情,但我决定创建一个为我们做这个的类。

它使用ObservableCollection<T>来跟踪对象/属性是否已在其列表中更改。

    public class ObservableList<T> : ObservableCollection<T>
    {
        public ObservableList() : base()
        {
            CollectionChanged += new NotifyCollectionChangedEventHandler(nObservableCollection_CollectionChanged);
        }

        public event PropertyChangedEventHandler OnPropertyChanged;

        void nObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (OnPropertyChanged != null)
            {
                OnPropertyChanged(new object[] { e.OldItems, e.NewItems }, null); // Call method to let it change the tabpages
            }
        }
    }

现在,我们必须创建一个帮助我们跟踪的帮助程序类:

    public class TabControlBind
    {
        public TabControlBind(TabControl tabControl)
        {
            // Create a new TabPageCollection and bind it to our tabcontrol
            this._tabPages = new TabControl.TabPageCollection(tabControl);
        }

        // Fields
        private ObservableList<TreeNodeItem> _treeNodeItems;
        private TabControl.TabPageCollection _tabPages;

        // Properties
        public ObservableList<TreeNodeItem> TreeNodeItems
        {
            get { return _treeNodeItems; }
            set
            {
                if (_treeNodeItems != value)
                {
                    _treeNodeItems = value;
                    _treeNodeItems.OnPropertyChanged += OnPropretyChanged;
                    OnPropretyChanged(null, null);
                }
            }
        }

        public TabControl.TabPageCollection TabPages
        {
            get
            {
                return this._tabPages;
            }
        }

        // Events
        private void OnPropretyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender == null) // If list got set
            {
                // Remove existing tabpages
                this._tabPages.Clear();

                // Loop through all items inside the ObservableList object and add them to the Tabpage
                foreach (TreeNodeItem _treeNodeItem in this._treeNodeItems)
                {
                    TabPage tabPage = new TabPage() { Text = _treeNodeItem.Value, Tag = _treeNodeItems };
                    this._tabPages.Add(tabPage);
                }
            }
            else if (sender is object[]) // If only one (or multiple) objects have been changed
            {
                // Get OldItems and NewItems
                object[] changedItems = (object[])sender;
                // Remove OldItems
                if (changedItems[0] != null)
                {
                    foreach (dynamic oldItems in (IList)changedItems[0])
                    {
                        foreach (TabPage tab in this._tabPages)
                        {
                            if (tab.Text == oldItems.Value)
                            {
                                this._tabPages.Remove(tab);
                                break;
                            }
                        }

                    }
                }
                // Add OldItems
                if (changedItems[1] != null)
                {
                    foreach (dynamic newItems in (IList)changedItems[1])
                    {
                        TabPage tabPage = new TabPage() { Text = newItems.Value, Tag = newItems };
                        this._tabPages.Add(tabPage);
                    }
                }
            }
        }
    }

这是一个如何使用它的示例:

    TabControlBind tabControlBinder;
    ObservableList<TreeNodeItem> treeNodeItems;

    private void btnAdd_Click(object sender, EventArgs e)
    {
        // This will automatically update the TabControl
        treeNodeItems.Add(new TreeNodeItem(new NTree<string>() { data = "Test3" }));
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
        // Create a new list object an add items to it
        treeNodeItems = new ObservableList<TreeNodeItem>();
        treeNodeItems.Add(new TreeNodeItem(new NTree<string>() { data = "Test" }));
        treeNodeItems.Add(new TreeNodeItem(new NTree<string>() { data = "Test2" }));

        // Create a new instance of the TabControlBind class, set it to our TabControl
        tabControlBinder = new TabControlBind(tabControl);
        tabControlBinder.TreeNodeItems = treeNodeItems;
    }