CBe*_*nni 4 .net c# propertygrid winforms
我在WinForms中有一个PropertyGrid控件(http://msdn.microsoft.com/en-us/library/aa302326.aspx).现在我想将中间垂直线向左移动(它总是居中,但是我的键非常短,而值是Paths,它们很长.控件在默认情况下将线放在中间,尽管用户很多为了方便用户,我想以编程方式将行更多地移到左边.我现在已多次搜索WinForms设计器属性以及PropertyGrid控件的成员,但未找到该选项(也没有任何与之相关的事件).
隐私是通过隐私来隐藏吗?我只是监督它吗?(在这种情况下,我真诚地抱歉)或者我怎么能这样做呢?
是的,不幸的是,这需要一些基于反射的黑客才能实现.这是一个示例扩展类:
PropertyGridExtensionHacks.cs
using System.Reflection;
using System.Windows.Forms;
namespace PropertyGridExtensionHacks
{
public static class PropertyGridExtensions
{
/// <summary>
/// Gets the (private) PropertyGridView instance.
/// </summary>
/// <param name="propertyGrid">The property grid.</param>
/// <returns>The PropertyGridView instance.</returns>
private static object GetPropertyGridView(PropertyGrid propertyGrid)
{
//private PropertyGridView GetPropertyGridView();
//PropertyGridView is an internal class...
MethodInfo methodInfo = typeof(PropertyGrid).GetMethod("GetPropertyGridView", BindingFlags.NonPublic | BindingFlags.Instance);
return methodInfo.Invoke(propertyGrid, new object[] {});
}
/// <summary>
/// Gets the width of the left column.
/// </summary>
/// <param name="propertyGrid">The property grid.</param>
/// <returns>
/// The width of the left column.
/// </returns>
public static int GetInternalLabelWidth(this PropertyGrid propertyGrid)
{
//System.Windows.Forms.PropertyGridInternal.PropertyGridView
object gridView = GetPropertyGridView(propertyGrid);
//protected int InternalLabelWidth
PropertyInfo propInfo = gridView.GetType().GetProperty("InternalLabelWidth", BindingFlags.NonPublic | BindingFlags.Instance);
return (int)propInfo.GetValue(gridView);
}
/// <summary>
/// Moves the splitter to the supplied horizontal position.
/// </summary>
/// <param name="propertyGrid">The property grid.</param>
/// <param name="xpos">The horizontal position.</param>
public static void MoveSplitterTo(this PropertyGrid propertyGrid, int xpos)
{
//System.Windows.Forms.PropertyGridInternal.PropertyGridView
object gridView = GetPropertyGridView(propertyGrid);
//private void MoveSplitterTo(int xpos);
MethodInfo methodInfo = gridView.GetType().GetMethod("MoveSplitterTo", BindingFlags.NonPublic | BindingFlags.Instance);
methodInfo.Invoke(gridView, new object[] { xpos });
}
}
}
Run Code Online (Sandbox Code Playgroud)
要移动拆分器位置,请使用MoveSplitterTo扩展方法.使用GetInternalLabelWidth扩展方法获取拆分器的实际位置.请注意,我观察到,在分配SelectedObject并且未显示PropertyGrid之前,GetInternalLabelWidth返回(-1).
样品用途:
using PropertyGridExtensionHacks;
//...
private void buttonMoveSplitter_Click(object sender, EventArgs e)
{
int splitterPosition = this.propertyGrid1.GetInternalLabelWidth();
this.propertyGrid1.MoveSplitterTo(splitterPosition + 10);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4538 次 |
| 最近记录: |