控制共享选定索引

控制共享选定索引

本文介绍了DataRepeater 上的 ComboBox 控制共享选定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据中继器,每行使用一个组合框.当您单击添加"按钮时,它会向 DataRepeater 中添加一个新行.

I have a data repeater with each row using a combobox. When you click the Add button it adds a new row to the DataRepeater.

   public enum ColorTypes {Red, Green, Yellow}
   class LineItem
   {
       public ColorTypes Color {get;set;}
   }

当您单击表单上的按钮时,它会创建一个新的 LineItem,然后重新绑定.

When you click a button on the form it then creates a new LineItem, and rebinds.

  private void btnAdd_Click(object sender, EventArgs e)
  {
       LineItem CopyItem = new LineItem();
       CurrentList= ((List<LineItem>)dataRepeater.DataSource);

       CurrentList.Add(CopyItem);
       dataRepeater.DataSource = CurrentList;
  }

这正确地向 DataRepeater 添加了一个新行,但现在更改组合框的索引会修改这两行.您需要执行一些特殊的绑定吗?

This correctly adds a new row to the DataRepeater, but now changing combobox's index modifies both rows. Is there some special binding you need to perform?

这是我的绑定代码:

 this.cbPackage.DataSource = System.Enum.GetValues(typeof(ColorTypes));
 this.cbPackage.DisplayMember = "Color";

推荐答案

经过大量搜索和整理后,您可以解决这个问题!为了在 DataRepeater 中正确使用组合框,您需要添加以下事件.

After a LOT of searching and putting things together, you CAN pull this off! In order to properly use a combo box in a DataRepeater you will need to add the following events.

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 项目克隆

MSDN DrawItem

现在当 ItemCloned 事件被调用时,你需要找到你的组合框控件并手动映射数据源(这可以防止下拉列表使用相同的源并共享相同的选定索引)

Now when the ItemCloned event is called you need to find your combo box control and manual map the data source (this prevents the dropdowns from using the same source and share the same selected index)

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

如果您的数据在添加行上发生更改,或者您需要重绘中继器,则组合框索引将设置为空.所以要修复它,在项目模板完成后,您将不得不在绘制时重置选定的索引.

If your data changes on an add row, or you need to redraw the repeater the combo box index will be set to null. So to fix it, after the item template is done you're going to have to reset the selected index on draw.

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

最后,当您更改下拉列表的值时,会出现最后一个问题.数据源不会自动更新.所以现在您需要根据组合框的索引手动更新源.

Finally, the last issue occurs when you change the value of the dropdown. The datasource does not get updated automatically. So now you need to manually update the source based of the index of the combobox.

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

哇……好了,一切都应该准备好了.

Wow... There you go, everything should be all set.

这篇关于DataRepeater 上的 ComboBox 控制共享选定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:42