Search code examples
c#silverlightwindows-phone-7delegatesrouted-events

Routed events in silverlight 3?


I have a Control, within a control, within a control.

Like so..

QuizMaster -> Question -> Answers -> RadioButton

When one of the answers is checked I want the function in Quizmaster called AskNextQuestion() to run.

How do I do that?


Solution

  • You would create an event in your nested control, and have your QuizMaster subscribe to that event.

    In your Answers add this:

    public static event Action<bool> IsAnswered;
    

    and Fire this event when you select a RadioButton in its handler

    public void OnRadioButtonSelected(object sender, SomeEventArgs e)
    {
      if(IsAnswered != null)
        IsAnswered(true);
    }
    

    and in your QuizMaster Subscribe to this static event:

    public void SomeMethod()
    {
      Answers.IsAnswered += new Action<bool>(Answers_IsAnsweredCompleted);
    }
    
    public void Answers_IsAnsweredCompleted(bool IsAsnwered)
    {
      //call your method in QuizMaster
    }