Since getting the field gridView
didn't work for me, I made another approach, where you can get the GridView from the Controls
property.
The GridView, however, is of type System.Windows.Forms.PropertyGridInternal.PropertyGridView
, which is, you guessed it, internal.
But, all we care about at this point is a control that actually has the method MoveSplitterTo
, so we enumerate the controls until we get what we want, and then just call the method.
public class PropertyGridEx : PropertyGrid
{
private int splitterPosition;
public int SplitterPosition
{
get => splitterPosition;
set
{
splitterPosition = value;
foreach (var control in Controls)
{
if (control.GetType().GetMethod("MoveSplitterTo", BindingFlags.Instance | BindingFlags.NonPublic) is MethodInfo method)
{
method.Invoke(control, new object[] { value });
break;
}
}
}
}
}