How to make a Stripped Down PropertyGrid
Asked Answered
O

1

3

I'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?

Ovipositor answered 21/2, 2011 at 17:57 Comment(0)
S
3

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!

Shy answered 1/3, 2011 at 8:49 Comment(1)
Well, it's better than nothing, thanks! I'll try it when I get a chanceOvipositor

© 2022 - 2024 — McMap. All rights reserved.