简体   繁体   English

用户控件的子级属性在“属性”窗口中

[英]User control's children properties in the Properties window

I have a composite user control containing a DataGridView. 我有一个包含DataGridView的复合用户控件。 In addition, the DataGridView.Modifiers property is set to Public . 此外, DataGridView.Modifiers属性设置为Public

When dropping my user control to the designer, I would like to see the properties of this inner DataGridView in the Properties window. 将用户控件放到设计器中时,我想在“属性”窗口中查看此内部DataGridView的属性。

What should I do? 我该怎么办? Many thanks. 非常感谢。

Your inner DataGridVIew needs to have an instance and a public property defined in the composite control via which it can be accessed. 您内部的DataGridVIew需要在复合控件中定义一个实例和一个公共属性,通过它可以对其进行访问。

Something like this: 像这样:

// your grid control instantiated somewhere:
DataGridVIew myInnerDataGridVIew = new ...

public DataGridVIew MyInnerDataGridVIew 
{ 
  get { return myInnerDataGridVIew; } 
  set { myInnerDataGridVIew = value; } 
}

Once you create this property and rebuild, you should see a MyInnerDataGridVIew member in the properties window when the composite control is selected in the designer, with a [+] next to it. 创建并重建此属性后,在设计器中选择复合控件时,应在属性窗口中看到MyInnerDataGridVIew成员,并在其旁边带有[+]。 Clicking the plus you should see the DataGridVIew's properties expanded. 单击加号,您应该看到DataGridVIew的属性已展开。

Even though the other two answers are correct too, I would argue that the more common way is to just allow read-only access to the reference of the instance of the inner grid. 即使其他两个答案也都是正确的,我还是认为更常见的方法是只允许对内部网格实例的引用进行只读访问。

public DataGridView DataGrid { get { return this.dataGridView1; } }

This way the user of your control can access all the grid properties and override whatever defaults you have chosen for it (like background color, etc…) but they cannot replace the grid w/ a new instance, which could possibly mess up some internal logic of your control. 这样,控件的用户可以访问所有网格属性并覆盖您为其选择的任何默认值(例如背景色等),但是他们无法替换带有新实例的网格,这可能会破坏一些内部逻辑由您控制。

What you need to do is add a public property so that any container you drag that control into will see it as a property. 您需要做的是添加一个公共属性,以便您将该控件拖动到的任何容器都将其视为属性。

Simply, lets assume you have 1 static data grid which a name of dataGrid1. 简单地说,假设您有1个静态数据网格,其名称为dataGrid1。 Now to make that accessible outside (and visible in the properties box) you need to make a property which will set and return it. 现在,要使该属性可在外部访问(并在属性框中可见),您需要创建一个将设置并返回它的属性。

public DataGridView ChildDataGridView
{
    get { return this.dataGrid1; }
    set { this.dataGrid1 = value; }
}

That will allow you to modify it from outside. 这样您就可以从外部对其进行修改。 Now when you add the control to the designer you should see a new property called ChildDataGridView when you have your user control selected. 现在,当您将控件添加到设计器中时,选择用户控件后,应该会看到一个名为ChildDataGridView的新属性。

You can modify and access it from that property. 您可以从该属性修改和访问它。

This will only work if the dataGrid1 is there all the time and is not generated dynamically in the code. 仅当dataGrid1一直存在并且不在代码中动态生成时,这才起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM