2

我正在使用 winform DevExpress 库。现在需要基于 PopupContainerEdit 创建一个控件,但是该控件必须具有一些行为,例如当它获得焦点时,弹出窗口打开,当失去焦点时弹出窗口关闭。

这是我正在使用的代码,但是在获得焦点后弹出窗口消失了。

public class HelpEdit : PopupContainerEdit {
    private PopupContainerControl _container;
    private GridControl _gridControl;
    private GridView _gridView;

    [DefaultValue("")]
    [DXCategory("Data")]
    [AttributeProvider(typeof(IListSource))]
    public object Datasource {
        get { return _gridControl.DataSource; }
        set { _gridControl.DataSource = value; }
    }

    public HelpEdit() : base() {
        _container = new PopupContainerControl();
        this.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
        this._gridControl = new GridControl();
        this._gridControl.Dock = DockStyle.Fill;
        this._gridView = new GridView(_gridControl);
        _container.Controls.Add(_gridControl);
        _container.Size = new Size(this.Width, 250);

        this.Properties.PopupControl = _container;
        this.Properties.PopupControl.Size = new Size(this.Width, 250);
    }
    protected override void OnGotFocus(EventArgs e) {
        base.OnGotFocus(e);
        this.ShowPopup();
    }
    protected override void OnLostFocus(EventArgs e) {
        base.OnLostFocus(e);
        this.ClosePopup();
    }
}
4

1 回答 1

3

你的弹出窗口消失了,因为一旦弹出容器 control( _container) 获得焦点,它就会被你的代码关闭。您不应该在 OnLostFocus() 覆盖中关闭弹出窗口,因为 is 的base.OnLostFocus方法PopupContainerEdit已经包含用于关闭弹出窗口的正确代码。或者使用以下代码有条件地关闭弹出窗口:

protected override void OnLostFocus(EventArgs e) {
    if(IsPopupOpen && !EditorContainsFocus) 
        ClosePopup(PopupCloseMode.Immediate);
    base.OnLostFocus(e);
}
于 2012-12-20T04:51:25.530 回答