2009-02-12 88 views
1

我试图执行以下操作: Winforms表单上的两个组合框,第一个组合框具有父类别列表,第二个组合框是父类别的子组, ,子列表根据父项中的选择更改内容。当DataSource是组合框上的数据绑定时,未引发SelectedItem/Index/ValueChanged事件

我想这样做正确使用数据绑定,但我发现与ComboBox控件一个奇怪的怪癖。

我设置手动母体的数据源:

cboParent.DataSource = ParentDataSource其中ParentDataSource是IList<ParentDTO>。 我然后可以在seletedItem绑定到DTO从而:

cboParent.DataBindings.Add(new Binding("SelectedItem", bindingSource, "Parent", true, DataSourceUpdateMode.OnPropertyChanged));在我的总体DTO一个ParentDTO对象结合Parent

迄今为止所有漂亮的标准。这工作,只要我选择列表中的新东西,写回我的DTO对象的变化,太棒了!

我那么孩子组合框的数据源绑定到总体DTO的列表: cboChild.DataBindings.Add(new Binding("DataSource", bindingSource, "Children", true, DataSourceUpdateMode.OnPropertyChanged));其中儿童是首要DTO的IList<ChildDTO>

这也工作得很好,只要我改变父母的选择,主持人改变DTO上的列表Children,并显示在cboChildren中的值发生变化,奇妙的我听到你哭泣(而我自己)!

不幸的是,如果您使用数据绑定在ComboBox上设置数据源,SelectedItemChanged,SelectedIndexChanged和SelectedValueChanged事件根本不会触发!这意味着OnProperyChanged数据绑定不适用于第二个组合框。 OnValidation确实有效,但对我来说似乎有点奇怪,我想知道是否有人曾经遇到过这种情况,并且他们是否已经制定了如何使其工作?

在此先感谢

斯图

回答

0

以及仍然需要帮助?您需要创建bindingSources和过滤器的辅助文本框和使用方便的特性bindingSource.filter

方法如下:

Dim ds As New DataSet 
Dim bind1 As New BindingSource 
Dim bind2 As New BindingSource 

'' here I add data to the dataset.. you MUST do your own populate way 
ds.Merge(GetProducts) ' returns a dataset filled with products 
ds.Merge(GetCategories) ' returns a dataset filled with categories 

'' after the ds has data 

' create binds 
bind1.DataSource = ds 
bind1.DataMember = "products" 

' crete binds 
bind2.DataSource = ds 
bind2.DataMember = "categories" 

txtMaster.DataSource = bind1 
txtMaster.DisplayMember = "product_name" 
txtMaster.ValueMember = "product_id" 

txtDetails.DataSource = bind2 
txtDetails.DisplayMember = "category_name" 
txtDetails.ValueMember = "category_id" 

txtAux.DataBindings.Add("Text", bind1, "product_id") ' bind1 contais products data 
' this perform a filter on bind2... that contains categories data 
Private Sub txtMaster_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaster.SelectedIndexChanged 
    bind2.Filter = "product_id = '" & txtAux.Text & "'" 
End Sub 

希望它能帮助

0

我已经得到的组合框几原因,例如支持绑定到空DTO的。 帮我插入此functinulity来修复绑定未更新的问题:

public new object SelectedItem 
{ 
    get 
    { 
     return base.SelectedItem; 
    } 
    set 
    { 
     base.SelectedItem = value; 
     if (value == null || value == System.DBNull.Value) 
     { 
      this.SelectedIndex = -1; 
     } 
     **foreach (Binding binding in DataBindings) 
     { 
      if (binding.PropertyName == "SelectedItem") 
      { 
       binding.WriteValue(); 
      } 
     }** 
    } 
} 

你可能想这样做也,如果属性名称是的SelectedValue或的selectedIndex。

如果这有助于某人,请回复!

最好的问候, Efy