C# PropertyGrid - Check if a value is currently be

2019-08-05 17:57发布

Is there a simple way to find out if a property grid is currently being edited by the user?

My usecase is the following: I update the grid data every second. If the user is editing a value, all inputs get lost when my update is called. So what I want to do is only update if the user is not editing something.

1条回答
Viruses.
2楼-- · 2019-08-05 18:10

I don't think there is any official way. However the following piece of code can detect when a grid entry is opened using the builtin text box editor, or the dropdown editor. It does not detect when an entry is opened using the small '...' edit button.

public static bool IsInEditMode(PropertyGrid grid)
{
    if (grid == null)
        throw new ArgumentNullException("grid");

    Control gridView = (Control)grid.GetType().GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(grid);
    Control edit = (Control)gridView.GetType().GetField("edit", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(gridView);
    Control dropDownHolder = (Control)gridView.GetType().GetField("dropDownHolder", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(gridView);

    return ((edit != null) && (edit.Visible & edit.Focused)) || ((dropDownHolder != null) && (dropDownHolder.Visible));
}

Of course, since it's based on the grid internal structure, it may change in the future, so, use at your own risk.

查看更多
登录 后发表回答