我在我的 C# 项目中使用 .NET PropertyGrid 控件。
加载包含网格的表单时,水平分隔符(将设置与描述分开)处于默认位置。如何在 C# 中以编程方式更改该拆分器的位置?
我在我的 C# 项目中使用 .NET PropertyGrid 控件。
加载包含网格的表单时,水平分隔符(将设置与描述分开)处于默认位置。如何在 C# 中以编程方式更改该拆分器的位置?
此代码基于 The Code Project 中的一篇文章 ( http://www.codeproject.com/KB/grid/GridDescriptionHeight.aspx ),其中引入了两个修复和一些清理。
private void ResizeDescriptionArea(PropertyGrid grid, int lines)
{
try
{
var info = grid.GetType().GetProperty("Controls");
var collection = (Control.ControlCollection)info.GetValue(grid, null);
foreach (var control in collection)
{
var type = control.GetType();
if ("DocComment" == type.Name)
{
const BindingFlags Flags = BindingFlags.Instance | BindingFlags.NonPublic;
var field = type.BaseType.GetField("userSized", Flags);
field.SetValue(control, true);
info = type.GetProperty("Lines");
info.SetValue(control, lines, null);
grid.HelpVisible = true;
break;
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex);
}
}
我在自己的项目中使用过它;它应该适合你。
您不能使用 PropertyGrid 控件公开的公共方法和属性来做到这一点,或者至少我找不到任何有用的东西。
您可能会尝试使用反射来获取显示设置或描述的属性网格的子控件,并尝试以编程方式设置它们的高度;我猜分离器只是停靠,设置它的位置不会改变任何东西。
使用调试器查看 PropertyGrid 的非公共成员应该可以帮助您了解控件的内部结构。
这是 Matthew Ferreira 在 VB.Net 中的解决方案。谢谢马修,请客!
Imports System.Reflection
Public Sub ResizeDescriptionArea(grid As PropertyGrid, lines As Integer)
Try
Dim info = grid.[GetType]().GetProperty("Controls")
Dim collection = DirectCast(info.GetValue(grid, Nothing), Control.ControlCollection)
For Each control As Object In collection
Dim type = control.[GetType]()
If "DocComment" = type.Name Then
Const Flags As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic
Dim field = type.BaseType.GetField("userSized", Flags)
field.SetValue(control, True)
info = type.GetProperty("Lines")
info.SetValue(control, lines, Nothing)
grid.HelpVisible = True
Exit For
End If
Next
Catch ex As Exception
Trace.WriteLine(ex)
End Try
End Sub