你的位置:首页 > ASP.net教程

[ASP.net教程]【原创】有关Silverlight中“DataGrid中级联动态绑定父/子ComboBox ”的示例。


尝试了很多种方案,由于Datagrid动态生成的每行父子comboBox的Name的不确定性,给父ComboBox绑定事件中获取 子ComboBox很难根据Name获取到。

花了不少时间和公司同事商讨,最终方案如下:

注:父子combobox分别代表: 主行业/子行业

 private void CurrentRowIndusrtylist_SelectionChanged(object sender, SelectionChangedEventArgs e)    {      ComboBox combobox = sender as ComboBox;      //获取另一个conbox控件      CurrentRowIndusrtylist = (sender as ComboBox);      if (CurrentRowIndusrtylist != null)      {         DataGridCell dg = (DataGridCell)(CurrentRowIndusrtylist.Parent);//关键用法        if (DataGridRow.GetRowContainingElement(dg) != null)        {           CurrentRowIndusrtySublist = this.dataGrid9.Columns[3].GetCellContent(DataGridRow.GetRowContainingElement(dg)) as ComboBox;//关键用法          if (CurrentRowIndusrtySublist != null)            BindSubIndusrtyCodes();//动态去绑定子combobox        }      }    }    private void BindSubIndusrtyCodes()    {      if (CurrentRowIndusrtylist.SelectedItem != null)      {        var unitIndusrtyItem = CurrentRowIndusrtylist.SelectedItem as System_IndustryCodes;        _db.Load(_db.GetIndustryCodesQuery(), op =>        {          CurrentRowIndusrtySublist.Items.Clear();          CurrentRowIndusrtySublist.Items.Add(new System_IndustryCodes { ID = -1, IndustryCodes = "--请选择子行业--" });          IEnumerable<System_IndustryCodes> tempsubList = op.Entities.Where(q => q.ParentCodeID == unitIndusrtyItem.ID);          foreach (var temp in tempsubList)          {            if (temp == null)              return;            CurrentRowIndusrtySublist.Items.Add(new System_IndustryCodes { ID = temp.ID, IndustryCodes = temp.IndustryCodes });          }          CurrentRowIndusrtySublist.DisplayMemberPath = "IndustryCodes";          CurrentRowIndusrtySublist.SelectedValuePath = "ID";          CurrentRowIndusrtySublist.UpdateLayout();          if (CurrentRowIndusrtySublist.Items.Count == 1)            CurrentRowIndusrtySublist.SelectedIndex = 0;          else            CurrentRowIndusrtySublist.SelectedIndex = 1;          //if (_OldSubindustryId != 0)            //CurrentRowIndusrtySublist.SelectedItem = (from q in CurrentRowIndusrtySublist.Items where (q as System_IndustryCodes).ID == _OldSubindustryId select q).FirstOrDefault();//默认选择项-子行业        }, null);      }    }

 

最终效果: