Platform / language, etc. it is not clear, therefore this answer is necessarily foggy ... however, in the general case, the child control cannot access the properties of the parent (directly), since many different types of parent controls can host control. The child should not be rigidly attached to one parent, otherwise he may be part of the parent.
Typically, you can try to simply remove this requirement - it sounds like an odd design. However, some frameworks support something like this - for example, dependency properties in WPF.
An interface-based design (for the parent (s)) is one approach, but it is not very clean. For .NET events, etc. - Another common way for a child to communicate with a parent - the child provides events that different parents can consume in different ways.
In addition, you enter the parent testing / casting area (either against the class or interface) to access the details from the parent - for example:
ParentControlType parent = this.Parent as ParentControlType; if(parent != null) {
(you can also use the interface here, still a bit hacked anyway ...)
Personally, instead of using this type of cast from the child, I would rather have the parent take control; the parent sets properties for the child and listens for events (and not the properties of the child set for the parent):
Thus, the child does not know or care about the parent. All he knows is that he has properties (etc.) and can notify other people about interesting changes.
source share