gpt4 book ai didi

c# - 将数据绑定(bind)到 Windows 窗体 TabControl

转载 作者:可可西里 更新时间:2023-11-01 08:13:02 25 4
gpt4 key购买 nike

我正在尝试我的第一个 Windows 窗体项目,之前完全基于 Web 并遇到了一些问题。我想将对象列表绑定(bind)到 TabControl 并让它创建选项卡,然后让数据绑定(bind)值可从每个选项卡的单击事件访问。

我要绑定(bind)的对象是

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 属性绑定(bind)到 Tab Text 属性。其他帖子提到绑定(bind)到控件的 ItemsSource 属性,但 Visual Studio 没有给我这个属性。

任何帮助将不胜感激。

干杯

斯图尔特

最佳答案

好吧,我不知道绑定(bind) 是必须的。尽管我从未见过在 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;
}

关于c# - 将数据绑定(bind)到 Windows 窗体 TabControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12194849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com