【问题标题】:How do you programmatically adjust the horizontal divider of a PropertyGrid control?如何以编程方式调整 PropertyGrid 控件的水平分隔线?
【发布时间】:2013-03-01 08:54:21
【问题描述】:

我在我的 C# 项目中使用 .NET PropertyGrid 控件。

加载包含网格的表单时,水平分隔符(将设置与描述分开)处于默认位置。如何在 C# 中以编程方式更改该拆分器的位置?

【问题讨论】:

    标签: c# .net visual-studio propertygrid gridsplitter


    【解决方案1】:

    此代码基于 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);
        }
    }
    

    我在自己的项目中使用过它;它应该适合你。

    【讨论】:

    • 工作得很好。在找到合适的值之前,我不得不对 Line 的值进行了相当多的调整,Line 所代表的并不是很直观,但是
    • 它对我不起作用。事实证明,在加载表单之前,此代码不起作用。在此之前,它会导致描述区域的高度为零。
    • 自从第一次提出这个问题以来已经有很长时间了,但我仍然在这里提供帮助。我在 Form.OnShown 中调用了这个函数。试试看,在这种情况下它应该可以工作。
    【解决方案2】:

    您不能使用 PropertyGrid 控件公开的公共方法和属性来做到这一点,或者至少我找不到任何有用的东西。
    您可能会尝试使用反射来获取显示设置或描述的属性网格的子控件,并尝试以编程方式设置它们的高度;我猜分离器只是停靠,设置它的位置不会改变任何东西。
    使用调试器查看 PropertyGrid 的非公共成员应该可以帮助您了解控件的内部结构。

    【讨论】:

      【解决方案3】:

      这是 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        • 2013-03-19
        • 1970-01-01
        相关资源
        最近更新 更多