【问题标题】:Edit DataGridView via Designer in inherited UserControl在继承的 UserControl 中通过 Designer 编辑 DataGridView
【发布时间】:2011-05-09 00:57:03
【问题描述】:

我创建了一个带有按钮、文本框和 DataGridView 的用户控件。 我将所有控件的修饰符设置为公共。

现在,如果我从那个 UserControl 继承,我可以通过 Designer 修改 TextBox 和 Button(四处移动,更改属性),就像我预期的那样。但是对于 DataGridView,所有属性都被禁用。

有什么原因,为什么我不能通过继承的 UserControl 中的设计器修改 DataGridView?

【问题讨论】:

  • 确实,这很奇怪。查看父用户控件后面的设计器创建的代码,我看不到任何锁定网格的东西。然而,当我从它继承并使用可视化编辑器时,控件在顶部显示一个小锁,表明某些东西已锁定控件。但是,我认为没有理由将其锁定,尽管必须这样做。听起来像是 DataGridView 控件设计者的问题。
  • 修复了这个问题。看看我的解决方案

标签: c# .net winforms windows-forms-designer


【解决方案1】:

在这里找到解决方案:http://adamhouldsworth.blogspot.com/2010/02/winforms-visual-inheritance-limitations.html

简而言之:

  1. 添加对 System.Design 的引用
  2. 使用以下代码从 DataGridView 继承:

    [Designer(typeof(ControlDesigner))]
    public class InheritableDataGridView : DataGridView
    {
        public InheritableDataGridView()
            : base()
        { }
    }
    
  3. ???
  4. 利润!!!

【讨论】:

  • 需要注意的一点:至少在我的特定情况下(我使用的是 VS2008),控件角落的小锁仍然出现,但是设计器中的属性是可编辑的。起初这让我很困惑,因为我认为解决方案不起作用。谢谢!
猜你喜欢
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 2011-04-12
  • 2020-01-25
  • 2010-09-21
  • 1970-01-01
  • 2010-10-27
相关资源
最近更新 更多