在我的WindowsForm窗体里放了一个Propertygrid,请问在runtime时如何让Propertygrid显示其他Control的扩展属性(ProvideProperty)?
以下是我的范例程序,如果我在Visual Studio 2005开发环境设计WindowsForm的画面中拉一个MyPanel迈来,并在其上放一个Label,当我选取该Label对象时,属性视窗(Property window)会显示该对象的属性,且会另附加上ControlIndex属性,但是我实际执行范例程序时,在执行时期的Propertygrid对象里,会显示label2对象的属性,可是没有附加上ControlIndex属性。请问我该如何修改,才能让Runtime的Propertygrid显示对象的附加属性(ProvideProperty)?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestProvideProperty
{
public partial class Form1 : Form
{
private PropertyGrid propertyGrid1 = new PropertyGrid();
public Form1()
{
InitializeComponent();
propertyGrid1.Dock = DockStyle.Fill;
this.Controls.Add(propertyGrid1);
}
private void Form1_Load(object sender, EventArgs e)
{
MyPanel panel1 = new MyPanel();
Label label1 = new Label();
label1.Text = "label1";
panel1.Controls.Add(label1);
Label label2 = new Label();
label2.Text = "label2";
panel1.Controls.Add(label2);
propertyGrid1.SelectedObject = label2;
}
}
[ProvideProperty("ControlIndex", typeof(Control))]
public class MyPanel : Panel, IExtenderProvider
{
[DisplayName("ControlIndex")]
public int GetControlIndex(Control sender)
{
return this.Controls.GetChildIndex(sender);
}
[DisplayName("ControlIndex")]
public void SetControlIndex(Control sender, int value)
{
this.Controls.SetChildIndex(sender, value);
}
bool IExtenderProvider.CanExtend(object extendee)
{
Control c = extendee as Control;
if (c != null)
{
return (c.Parent == this);
}
return false;
}
}
}