如何使子控件随父控件的更改而更新呢?
我有一些子控件托管在我的父对象中,我能够想到两种方法向它们发送更改信息:
1)将子控件连接到父控件中的事件,并在父控件更改时触发该事件
2)将子控件列表保存在数组中,并在发生更改时通过迭代数组来调用子控件中的方法以处理新的更改。
希望我描述得清楚。
这两种方法都可以工作,但处理方法可能有正确和错误之分。
编辑: 以下是我的代码...用于事件方法。我对如何将子级连接到父级不满意;有什么建议吗?
父控件...
public class A_Parent
{
public delegate void DelUpdateMessage( string msg );
public event DelUpdateMessage UpdateMessage;
public A_Parent()
{
a_Child1.prnt = this;
a_Child2.prnt = this;
a_Child3.prnt = this;
a_Child4.prnt = this;
}
private void FireUpdateMessageEvent( string message)
{
var handlers = UpdateMessage;
if (handlers != null)
handlers( message );
}
}
The child...
public class A_Child
{
A_Parent pnt;
public A_Parent prnt
{
set
{
pnt = value;
pnt.UpdateMessage += new A_Parent.DelUpdateMessage(pnt_UpdateMessage);
}
}
void pnt_UpdateMessage(string msg) { }
}