我在我的C#项目中使用.NET PropertyGrid控件。如何以编程方式调整PropertyGrid控件的水平分隔符?
当包含网格的表单加载时,水平分割器(将“设置”与“描述”分开)位于默认位置。如何在C#中以编程方式更改该分离器的位置?
我在我的C#项目中使用.NET PropertyGrid控件。如何以编程方式调整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
工作很好。在我能找到一个很好的价值之前,我不得不使用Line的值,但它并不真正直观地表示Line是什么,但是低于10的数字似乎是我需要的 – PICyourBrain 2010-02-15 15:05:38
它对我没有任何作用。事实证明,这个代码在加载Form之前不起作用。在此之前,它会使描述区域为零。 – Qwertie 2010-09-21 20:44:29
这是第一次问这个问题已经很长时间了,但我仍然在这里提供帮助。我在Form.OnShown中调用了这个函数。试试看,它应该在这种情况下工作。 – 2010-09-22 14:19:18