【问题标题】:Winform BindingSources - QuestionWinform BindingSources - 问题
【发布时间】:2010-01-15 00:55:04
【问题描述】:

我有一个 Windows 窗体 (net 2.0),其上的控件通过 BindingSource..works 绑定到实体 (w/INotifyPropertyChanged)。

在同一个表单上,我有一个下拉列表,它也通过 BindingSource..works 连接起来

以下是相关代码的示例:

m_PlanItemLookupBindingSource.DataSource = GetBusinessLogic().RetrievePaymentPlanLookups(); // Collection of PaymentPlans
paymentPlanType.Properties.DataSource = m_PlanItemLookupBindingSource;
paymentPlanType.Properties.DisplayMember = "Name";
paymentPlanType.Properties.ValueMember = "ID";
paymentPlanType.DataBindings.Add(new Binding("EditValue", m_PlanBindingSource, "PaymentPlanID", true, DataSourceUpdateMode.OnPropertyChanged, null, "D"));

agencyComission.DataBindings.Add(new Binding("EditValue", m_PlanBindingSource, "AgencyCommission", true, DataSourceUpdateMode.OnPropertyChanged, null, "P1"));
billingType.DataBindings.Add(new Binding("Text", m_PlanBindingSource, "BillingType"));

因此,当我更改下拉列表中的值时,我认为 m_PlanItemLookupBindingSource Current 属性会随着发生更改的实体的 PaymentPlanID 属性一起更改。

有点困惑。

提前致谢, 斯蒂芬

【问题讨论】:

    标签: winforms data-binding


    【解决方案1】:

    BindingSource 获取控件中的值并将其设置在底层源中,即由 BindingSource 的 Position 属性确定的当前对象。

    因此,当您在下拉列表中选择一个值时,基础对象的 PaymentPlanID 属性将设置为所选的新值。底层对象由 BindingSource 中的 Current 属性标识。

    如果要将 Current 属性移动到您在下拉列表中选择的对象,则必须使用 MoveFirst、MoveLast、MovePrevious 或 MoveNext 方法或 BindingSource 上的 Position 属性。

    正如我所见,您可以执行以下操作:在下拉列表中 Changed 或 ValueChanged 事件的事件处理程序中,您将获取所选项目的索引,您可以将该索引传递给 BindingSource.Position属性。

    Changed or ValueChanged event handler
        ...
        int index = DropDownList.ListIndex
        BindingSource.Position = index
        ...
    End event handler
    

    您应该从下拉列表 EditValue 中删除将下拉列表的 se 链接到 PaymentPlanID 的 DataBinding。这样,在 BindingSource 中的 Position 更改之前,基础对象中的 PaymentPlanId 不会设置为所选值。

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      相关资源
      最近更新 更多