DevExpress select 全部在单击 GridView 单元格时(文本编辑)

DevExpress select all when clicked on GridView cell (TextEdit)

我需要这样做,以便当用户在网格视图中单击带有 TextEdit 的单元格时,它会 select 全部在 textedit 中。我尝试了很多可以在互联网上找到的方法,但是 none 效果很好。

"EditorShowMode = MouseUp" 方式破坏了一切,例如,当您单击已选中它的单元格时;它 select 是单元格,然后您需要再次单击才能真正单击 CheckEdit。

"Use EditorShowMode = MouseUp and manually handle other things on MouseDown" 只是 ew。不适用于所有类型的控件。

"Change selection length etc. on ShownEditor event" 方法也不起作用,实际上它 select 是单击时的文本,但它不会覆盖默认功能,因此 selection 会立即更改。还尝试了 SelectAll 方法,但它有一些我不记得的问题(可能根本不起作用)。

我确实尝试了很多东西,但找不到完全好的方法。请告诉我您是否可以在不破坏网格中其他类型控件的情况下获得工作方式。

您可以使用 GridView CustomRowCellEdit 事件并设置文本编辑器的事件,例如 Mouse Up。设置RepositoryItemTextEdit MouseUp 事件可以像例子中那样设置。

示例:

private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
    if (e.RepositoryItem is DevExpress.XtraEditors.Repository.RepositoryItemTextEdit)
    {
        DevExpress.XtraEditors.Repository.RepositoryItemTextEdit rep = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
        rep.ReadOnly = false;
        rep.MouseUp += rep_MouseUp;
        e.RepositoryItem = rep;
    }       

}
void rep_MouseUp(object sender, MouseEventArgs e)
{
    DevExpress.XtraEditors.TextEdit te = sender as DevExpress.XtraEditors.TextEdit;
    te.SelectAll();
}

您应该处理 TextEdit 的 Enter 事件

private void myRepositoryItemTextEdit_Enter(object sender, EventArgs e)  
{  
    var editor = (DevExpress.XtraEditors.TextEdit)sender;  
    BeginInvoke(new MethodInvoker(() =>  
    {  
        editor.SelectionStart = 0;  
        editor.SelectionLength = editor.Text.Length;  
    }
}

Pavel 在 DevExpress 支持上的回答(效果很好):

The easiest way to achieve this is to use the GridView.ShownEditor event to subscribe to the active editor's MouseUp event. Then, select all text in the MouseUp event handler and detach this handler to avoid subsequent text selection.

private void GridView_ShownEditor(object sender, EventArgs e)
{
    GridView view = sender as GridView;
    if (view.ActiveEditor is TextEdit)
        view.ActiveEditor.MouseUp += ActiveEditor_MouseUp;
}

private void ActiveEditor_MouseUp(object sender, MouseEventArgs e)
{
    BaseEdit edit = sender as BaseEdit;
    edit.MouseUp -= ActiveEditor_MouseUp;
    edit.SelectAll();
}