Codebye.com首发总结一下使用DevExpress ComboBoxEdit 控件的过程中的问题和用法,ComboBoxEdit功能类似微软Winforms自带的ComboBox控件,但是使用过程中并未找到SelectionChangeCommitted事件。
这个事件只会在用户改变SelectedItem时才会触发,如果SelectedItem在代码中被更改时将不会触发SelectionChangeCommitted事件。(关于ComboBox SelectionChangeCommitted事件的用法请参考此链接:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted%28v=vs.110%29.aspx)
这里提供一种ComboBoxEdit 版的SelectionChangeCommitted事件实现方式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
namespace DXSelectionChangeCommitted
{
[ToolboxItem(true)]
public class MyXtraComboBoxEdit : ComboBoxEdit
{
private bool bUserChangedValue = false;
public event EventHandler SelectionChangeCommitted;
protected override void UpdateEditValueOnClose(PopupCloseMode closeMode, bool acceptValue, object newValue, object oldValue)
{
// newValue and oldValue are always the same ?www.Codebye.com 首发原创文章
if (acceptValue && newValue != EditValue)
{
bUserChangedValue = true;
}
base.UpdateEditValueOnClose(closeMode, acceptValue, newValue, oldValue);
}
protected override void UpdatePopupEditValueIndex(int prevIndex)
{
base.UpdatePopupEditValueIndex(prevIndex);
if (SelectionChangeCommitted == null || !bUserChangedValue)
return;
bUserChangedValue = false;
SelectionChangeCommitted(this, new EventArgs());
}
protected override void ScrollValue(int delta, bool cycle)
{
base.ScrollValue(delta, cycle);
if (SelectionChangeCommitted == null)
return;
bUserChangedValue = false;
SelectionChangeCommitted(this, new EventArgs());
}
}
}
如此这般即可实现ComboBoxEdit 的SelectionChangeCommitted事件。