简体   繁体   中英

How can I change parent control's content though child control event?

I have three user controls. ControlC is parent of ControlB, ControlB is parent of ControlA.Now I wan to use a ControlA's event to change ControlC's content(let's say change ControlC's content to ControlD). I can do this if there are only 2 Controls, I don't know to do it with 3 Controls. Any idea with it? For 2 controls solution is:

        private void ChangeParentContent()
    {
        UserControlB userControlB = (UserControlB)this.Parent;
        userControlB.Content = new userControlC();
    }

EDIT

After re-reading your question a couple of times, I think I better understand what you're going for. @Servy is correct, you should not modify the parent from the child. Instead, create events that the parents can subscribe to. When the event is fired you can handle the event in the parent. My original answer (below) provides for a couple of paths you could use to achieve this.

ORIGINAL

A couple of ideas:

  1. ControlB could subscribe to the ControlA event. ControlB then fires its own event, to which ControlC is subscribed.
  2. Change the Modifier of ControlA to public so that you can access it directly from ControlC ( ControlB.ControlA.Event += ... )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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