我似乎无法弄清楚这一点,也找不到任何答案。
我有一个绑定到模型中的属性的组合框。我将在我的代码中复制并粘贴关键行:
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 组合框。
我该如何解决这个问题?