3

我似乎无法弄清楚这一点,也找不到任何答案。

我有一个绑定到模型中的属性的组合框。我将在我的代码中复制并粘贴关键行:

       this.m_typeCombobox.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.m_bindingSource, "Type", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

我的模型:

public class TypeConfig : INotifyPropertyChanged
{     
            public event PropertyChangedEventHandler PropertyChanged;
           private EnumType<eLType> m_type;
    public EnumType<eLType> Type
    {
        get { return m_type; }
        set
        {
            if (m_type!= value)
            {
                m_type= value;
                var handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs("Type"));
            }
        }
    }

我需要在组合框 EditValueChanged 事件上更新模型,但看起来模型稍后会更新。EditValueChanged 是更改时最近调用的事件。

我试过这个:

void m_TypeCombobox_EditValueChanged(object sender, EventArgs e)
    {
           m_bindingSource.EndEdit(); //this doesn't work 
          //need to have the new value here
    }

这是 MSDN 所说的:

调用 EndEdit 方法时,所有挂起的更改都将应用于基础数据源。除非数据源包含的对象实现 IEditableObject 接口,否则此方法无效。如果对象未实现 IEditableObject 接口,则对数据的更改会在每次更改后立即复制到基础数据源。

因此,根据我的理解,模型应该在更改组合框值时立即更新。

我正在使用与普通 WinForms 组合框几乎相同的 DevExpress 组合框。

我该如何解决这个问题?

4

2 回答 2

0

尝试绑定到“Value”属性,而不是“EditValue”我希望它可以帮助你

于 2012-12-18T15:22:12.880 回答
0

要让 BindingSource.EndEdit 执行任何操作,您需要为 BindingSource 中包含的项目实现 System.ComponentModel.IEditableObject。

当您在绑定源上调用“EndEdit”时,它随后会在其列表中实现 IEditableObject 的项目上调用相应的 IEditableObject.EndEdit() 方法。

话虽如此,我遇到了一些问题,例如,当用户关闭表单时,没有为所有已调用 BeginEdit 的项目调用 EndEdit。

于 2013-02-05T02:27:25.847 回答