-1

I've followed this question and tried to build my solution. The problem is that 'UserControlButtonClicked' appears to be null! So 'UserControlButtonClicked(this, EventArgs.Empty)' inside the if, doesn't run, and the method 'addStepContent' in the parent page is never called.

UserControl 'StepsBar'

public sealed partial class StepsBar : UserControl
    {

        public event EventHandler UserControlAddStepContent;

        [...]

    public StepsBar()
    {
        this.InitializeComponent();
                    Image step_1 = new Image();

        ButtonInfo step_1Info = new ButtonInfo();
        step_1Info.Add((int)stepNumber.one, (int)stepStatus.normal);
        step_1.Tag = step_1Info;

        step_1.Source = setBackground((int)stepStatus.normal);
        step_1.Tapped += stepTapped;

        [...]
    }

public void stepTapped(Object sender, RoutedEventArgs e)
    {
        [...]

        if (step != null)
        {

           [...]

            firePageEvent(); 

        }

    }

    public void firePageEvent() 
    {
        if (UserControlAddStepContent != null)
        {
            UserControlAddStepContent(this, EventArgs.Empty); 
        }
    }

Parent Page

public Violation()
    {

        this.InitializeComponent();

        StepsBar stepsBar = new StepsBar();

        stepsBar.UserControlAddStepContent += new EventHandler(addStepContent); 


    }

    private void addStepContent(object sender, EventArgs e) 
    {

        CheckBox check_1 = new CheckBox();
        check_1.Content = "Check me!";
        bodyStackPanel.Children.Add(check_1);

    }
Community
  • 1
  • 1
Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64

2 Answers2

-1

This assumes that you want to use an existing delegate rather than make your own and you aren't passing anything specific to the parent page by event args.

In the user control's code-behind (adapt as necessary if not using code-behind or C#):

   public partial class MyUserControl : System.Web.UI.UserControl
    {
        public event EventHandler UserControlButtonClicked;


    private void OnUserControlButtonClick()
    {
        if (UserControlButtonClicked != null)
        {
            UserControlButtonClicked(this, EventArgs.Empty);
        }
    }

    protected void TheButton_Click(object sender, EventArgs e)
    {
        // .... do stuff then fire off the event
        OnUserControlButtonClick();
    }

    // .... other code for the user control beyond this point
}

In the page itself you subscribe to the event with something like this:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // hook up event handler for exposed user control event
        MyUserControl.UserControlButtonClicked += new  
                    EventHandler(MyUserControl_UserControlButtonClicked);
    }
    private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
    {
        // ... do something when event is fired
    }

}
tam tam
  • 1,870
  • 2
  • 21
  • 46
  • as I wrote in the question, I use the exact example you paste, but instead of working: UserControlButtonClicked' appears to be null! So 'UserControlButtonClicked(this, EventArgs.Empty)' inside the if, doesn't run. – Massimo Variolo Jan 16 '13 at 17:37
  • I only provided you an example which works as expected and a method on a parent page can be accessed. – tam tam Jan 16 '13 at 18:26
-1

Solved. The problem was this, on the parent page.

StepsBar stepsBar = new StepsBar();

    stepsBar.UserControlAddStepContent += new EventHandler(addStepContent); 

The istance of StepsBar was not added to the page. D'OH! So here's what I've done:

    stepsBar.UserControlAddStepContent += new EventHandler(addStepContent); 

and on the xaml of the parent page:

<local:StepsBar x:Name="stepsBar"/>
Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64