C# 嵌套绑定源父子关系

本文关键字:父子关系 绑定 嵌套 | 更新日期: 2023-09-27 18:33:10

为了使我的问题更容易理解,假设我们有一个包含 2 个组合框(父组合和子组合)的表单。选择父组合框时,子组合框的项目也会相应更改。州县就是一个很好的例子。

下面是数据模型。

public class item
{
    public string childname { get; set; }
    public item(string n) { childname = n; }
}
public class itemparent
{
    public BindingSource<item> Children { get; set; }
    public string parentName { get; set; }
    public itemparent(string parentname,item n)
    {
        Children = new BindingSource<item>();
        this.parentName = parentname;
    }
}

public class BindingSource<T> : BindingSource
{
    public new T Current
    {
        get
        {
            return (T)base.Current;
        }
    }
}

以下是我如何绑定它们。

        public BindingSource<itemparent> bsource { get; set; }
    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            this.bsource = new BindingSource<itemparent>()
            {
                new itemparent("parent Henry",new item("child name Marry")) ,
                new itemparent("parent Todd",new item("child name Alex")) 
            };
            //this works fine
            var bnd = new Binding("DataSource", this, "bsource");
            this.combo_parents.DataBindings.Add(bnd);
            this.combo_parents.DisplayMember = "parentName";
             //not working as i expect it, does not uplate data source if the above combo changes
            //and i cannot bind to bsource.Children.Current (throws exception complaining that Current prop no found)
            combo_children.DataBindings.Add("DataSource", this, "bsource.Children");
            combo_children.DisplayMember = "childname";
        }
        catch (Exception ex)
        {
            Debugger.Break();
        }
    }
}

我也想为孩子们使用 bindingsource.current,并且能够做到:

 Get Selected Parent Item
 bsource.Current
 Get Selected Child item
 bsource.Current.Children.Current

我知道还有其他方法可以做到这一点,但我发现这种方法是最干净的。执行绑定,然后在任何时候使用 BindingSource,您可以获取所选项目。此试用的完整VStudio解决方案可在此处获得。

C# 嵌套绑定源父子关系

有趣的想法。但是您需要考虑一个细节 - 您不能绑定到列表对象的属性。所以你需要稍微改变你的设计。

首先,自定义绑定源类不应继承BindingSource,但应具有如下所示的BindingSource属性

public class BindingSource<T>
{
    public BindingSource() { Source = new BindingSource(); }
    public BindingSource Source { get; set; }
    public T Current { get { return (T)Source.Current; } }
    public event EventHandler CurrentChanged
    {
        add { Source.CurrentChanged += value; }
        remove { Source.CurrentChanged -= value; }
    }
}

那么绑定将是这样的

public BindingSource<itemparent> bsource { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
    this.bsource = new BindingSource<itemparent>
    {
        Source = new BindingSource
        {
            new itemparent("parent Henry",new item("child name Marry")) ,
            new itemparent("parent Todd",new item("child name Alex"))
        }
    };
    this.combo_parents.DataBindings.Add("DataSource", this, "bsource.Source");
    this.combo_parents.DisplayMember = "parentName";
    this.combo_children.DataBindings.Add("DataSource", this, "bsource.Current.Children.Source");
    this.combo_children.DisplayMember = "childname";
}