简体   繁体   English

WinForms主从细节的程序化绑定

[英]WinForms programmatic binding of master-detail

I've got a WinForms app with an Explorer style TreeView on the left. 我有一个WinForms应用,左侧有一个Explorer类型的TreeView。 Selecting different nodes will display different UserControls composed of various other standard controls. 选择不同的节点将显示由各种其他标准控件组成的不同的UserControl。 All of the controls within the various UserControls are bound to a single DataSet. 各种UserControl中的所有控件都绑定到单个DataSet。 I have a ComboBox above the scope of the tree (to change between test and live settings) bound a master table, and as I have master-detail relationships setup between the tables, changing the ComboBox changes the current row for all child tables. 我在树的范围上方有一个ComboBox(用于在测试设置和实时设置之间进行更改),它绑定了一个主表,并且由于我在表之间设置了主从关系,因此更改ComboBox会更改所有子表的当前行。 So far, so good. 到现在为止还挺好。

I've now added plugable sub-trees (picked up from separate DLLs), and each sub-tree uses it's own DataSet. 现在,我添加了可插入的子树(从单独的DLL中拾取),每个子树都使用它自己的DataSet。 The part I'm having trouble with is how to make the sub-trees link to the master table. 我遇到的问题是如何使子树链接到主表。 I've replicated the master table within the sub-trees' DataSets (because the relations won't work between DataSets), but because the sub-trees don't have their own ComboBox, I'm unsure how to make the binding work. 我已经在子树的数据集中复制了主表(因为数据集之间的关系将不起作用),但是由于子树没有自己的ComboBox,因此我不确定如何使绑定起作用。 I guess I need a CurrencyManager for each sub-tree, and somehow link them to the ComboBox's change event, but how do I relate the sub-tree's UserControls to the CurrencyManager? 我猜我每个子树都需要一个CurrencyManager,并以某种方式将它们链接到ComboBox的change事件,但是如何将子树的UserControls与CurrencyManager相关联?

The piece I was missing was creating a BindingContext for the sub-tree... 我所缺少的那一部分是为子树创建BindingContext。

bindingContext = new BindingContext();

...and then setting it on the sub-tree's UserControls: ...然后将其设置在子树的UserControls上:

public partial class MyUserControl : UserControl
{
    public MyUserControl(BindingContext bindingContext, DataSet dataSet)
    {
        this.BindingContext = bindingContext;
        InitializeComponent();
        myTextBox.DataBindings.Add("Text", dataSet, "Master.Master_Detail.DetailField");
    }
}

I didn't understand that the CurrencyManager doesn't need to be explicitly created to link with the DataSet. 我不了解不需要显式创建CurrencyManager来与DataSet链接。 Setting the DataBindings is enough. 设置DataBindings就足够了。

Keeping the two separate DataSets in step then just required a method like this: 然后,将两个单独的数据集保持在同一步,则只需要这样的方法:

public void MasterChanged(long masterID)
{
    var currencyManager = bindingContext[dataModel.DataSet, "Master"];
    var masterRows = masterTable.Rows;
    for (int position = 0; position < masterRows.Count; position++)
    {
        if (((long)masterRows[position]["ID"]) == masterID)
        {
            currencyManager.Position = position;
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM