Skip to main content
 首页 » 编程设计

c#之更改 PropertyGrid 左侧集合编辑器/ View 的宽度

2024年02月03日27thcjp

任何带有长字符串的内容都会简单地引入一个带有滚动条的不可用 View 。

集合编辑器的宽度是否由设计固定,是否可以将拆分器引入到这个很棒的演示文稿中?

请您参考如下方法:

我还没有看到使用常规 PropertyGrid 执行此操作的方法,但如果您不介意付费,Visualhint 提供了更先进的产品 here - 也许试试吧。


这使用反射来完成工作;谨慎使用...

using System; 
using System.Reflection; 
using System.Windows.Forms; 
class Program { 
    [STAThread] 
    static void Main() { 
        Application.EnableVisualStyles(); 
        Form form = new Form(); 
        // this bar will control the splitter 
        ScrollBar sb = new HScrollBar { 
            Minimum = 10, Maximum = 200, 
            Dock = DockStyle.Bottom 
        }; 
        // the grid we want to control 
        PropertyGrid grid = new PropertyGrid { 
            SelectedObject = form, Dock = DockStyle.Fill 
        }; 
        // add to the form 
        form.Controls.Add(grid); 
        form.Controls.Add(sb); 
        // event to update the grid 
        sb.ValueChanged += delegate { 
            MoveSplitterTo(grid, sb.Value); 
        }; 
        Application.Run(form); 
    } 
    static void MoveSplitterTo(PropertyGrid grid, int x) { 
        // HEALTH WARNING: reflection can be brittle... 
        FieldInfo field = typeof(PropertyGrid) 
            .GetField("gridView", 
                BindingFlags.NonPublic | BindingFlags.Instance); 
        field.FieldType 
            .GetMethod("MoveSplitterTo",  
                BindingFlags.NonPublic | BindingFlags.Instance) 
            .Invoke(field.GetValue(grid), new object[] { x }); 
    } 
}