经过大量搜索和整理后,您可以完成此任务!为了在 DataRepeater 中正确使用组合框,您需要添加以下事件。
this.dataForm.ItemCloned += new Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventHandler(dataForm_ItemCloned);
this.dataForm.DrawItem += new Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventHandler(dataForm_DrawItem);
this.cbPackage.SelectedIndexChanged += new System.EventHandler(cbColor_SelectedIndexChanged);
MSDN ItemCloned
MSDN 绘图项
现在,当调用 ItemCloned 事件时,您需要找到组合框控件并手动映射数据源(这可以防止下拉菜单使用相同的源并共享相同的选定索引)
void dataForm_ItemCloned(object sender, Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs e)
{
var combo = (ComboBox)e.DataRepeaterItem.Controls.Find("cbColor", false)[0];
//Set the data source
Combo.DataSource = System.Enum.GetValues(typeof(DataRepeater.ColorTypes));
}
如果您的数据在添加行上发生更改,或者您需要重新绘制中继器,则组合框索引将设置为空。所以要修复它,在项目模板完成后,您将不得不在绘制时重置选定的索引。
/// <summary>
/// After Item is cloned, draw item. The index is now available to select the proper data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void dataForm_DrawItem(object sender, Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs e)
{
var dataRepeater = (Microsoft.VisualBasic.PowerPacks.DataRepeater)sender;
var combo = (ComboBox)e.DataRepeaterItem.Controls.Find("cbPackage", false)[0];
//Set the selected item based of the list item index
Combo.SelectedItem = ((List<LineItem>)DataRepeater.DataSource)[e.DataRepeaterItem.ItemIndex].PackageType;
}
最后,当您更改下拉列表的值时,会出现最后一个问题。数据源不会自动更新。所以现在您需要根据组合框的索引手动更新源。
void cbPackage_SelectedIndexChanged(object sender, System.EventArgs e)
{
var combo = (ComboBox)sender;
var dataRepeaterItem = (Microsoft.VisualBasic.PowerPacks.DataRepeaterItem)combo.Parent;
var dataRepeater = (Microsoft.VisualBasic.PowerPacks.DataRepeater)combo.Parent.Parent;
var source = ((List<ColorData>)DataRepeater.DataSource)[DataRepeaterItem.ItemIndex];
source.PackageType = (DropData)combo.SelectedValue;
}
哇... 好了,一切都应该准备好了。