Winforms ComboBox用户控件绑定
本文关键字:绑定 控件 用户 ComboBox Winforms | 更新日期: 2023-09-27 18:12:51
我有一个继承自Combobox控件的用户控件。我想在用户控件的构造函数中绑定数据。但是当我将它添加到表单并运行项目时,它显示了重复的项。
当我在winform中添加控件时,它会在窗体的设计器文件中添加项,当我运行项目时,它会在用户控件的构造函数中再次添加项。
public partial class CheckSeriesBox : ComboBox
{
private static List<string> CheckSeries;
public CheckSeriesBox()
{
InitializeComponent();
CheckSeries = new List<string>();
SetCheckSeries();
this.Items.AddRange(CheckSeries.ToArray());
this.SelectedIndex = 0;
}
public static List<string> SetCheckSeries()
{
CheckSeries.Add("A");
CheckSeries.Add("B");
}
}
http://social.msdn.microsoft.com/forums/vstudio/en-US/3e35b534-7d3f-4832-8859-b5cb838bd62a/extended-combobox-adds-items-twice
public partial class CheckSeriesBox : ComboBox
{
private static List<string> CheckSeries;
public CheckSeriesBox()
{
InitializeComponent();
CheckSeries = new List<string>();
SetCheckSeries();
if (DesignMode)
{
this.Items.AddRange(CheckSeries.ToArray());
}
}
public static List<string> SetCheckSeries()
{
CheckSeries.Add("A");
CheckSeries.Add("B");
}
protected new bool DesignMode
{
get
{
if (base.DesignMode)
{
return true;
}
else
{
Control parent = this.Parent;
while ((parent != null))
{
System.ComponentModel.ISite site = parent.Site;
if ((site != null) && site.DesignMode)
{
return true;
}
parent = parent.Parent;
}
return false;
}
}
}
}