【问题标题】:Force BindingSource to update data/model强制 BindingSource 更新数据/模型
【发布时间】:2012-12-18 15:17:17
【问题描述】:

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

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

       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 接口,则对数据的更改会在每次更改后立即复制到基础数据源。

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

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

我该如何解决这个问题?

【问题讨论】:

标签: c# user-interface devexpress


【解决方案1】:

尝试绑定到“Value”属性,而不是“EditValue” 希望对你有帮助

【讨论】:

  • 似乎这种类型的 DevExpress.XtraEditors.ComboBoxEdit 不提供 Value 属性。我必须使用 Editvalue。
【解决方案2】:

要让 BindingSource.EndEdit 做任何事情,您需要为 BindingSource 中包含的项目实现 System.ComponentModel.IEditableObject。

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

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

【讨论】:

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