开发者

How to make a Stripped Down PropertyGrid

I开发者_如何学JAVA'm building an app where I show data in a tree view, and when a user double-clicks on an element in the tree view, the node they clicked on is replaced with an editable version of the node. The way different nodes are edited varies greatly, so using the built-in ability to change the text of the node is not acceptable. I instead need to use a property grid and define [Editor] attributes.

The only problem is that the property grid shows two columns: One with the name of the property, and the other with its value. I need to only show the value column (the part the user can edit). Is there some way to remove the first column, or use the property grid's functionality in a different, custom class that only shows one column?


This is not possible without hacking the property grid. Here is a code that can change the label column's width:

public static void SetLabelColumnWidth(PropertyGrid grid, int width)
{
    if (grid == null)
        throw new ArgumentNullException("grid");

    // get the grid view
    Control view = (Control)grid.GetType().GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(grid);

    // set label width
    FieldInfo fi = view.GetType().GetField("labelWidth", BindingFlags.Instance | BindingFlags.NonPublic);
    fi.SetValue(view, width);

    // refresh
    view.Invalidate();
}

public static void ResetLabelColumnWidth(PropertyGrid grid)
{
    SetLabelColumnWidth(grid, -1);
}

Use it just like this to remove the label column:

    SetLabelColumnWidth(propertyGrid1, 0);

The reset function restores the label column.

Of course, it's a hack so it may not work in the future. There are also issues:

  • The v-splitter cursor is shown when the mouse moves to the grid's left side, and the user can select it and reset the label column if he clicks.
  • Some grid actions may also restore the label column (like using the property grid toolbar).

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜