title: C# State pattern and events
body: <p>In C#, events can only be fired by the owning class through protected virtual methods such as OnClick, OnExit, etc... What I'm trying to do is implementing GOF State Pattern for some of my complex classes. Everything worked great so far except that I can't find a way to fire an event of the owning class from my state class.</p>
  
  <p>For example, my button has two states (up and down) and the DownState class will check for user input and if necessary, fire the button's Click event.</p>
  
  <pre><code>public class Button
  {
      public Button()
      {
          State = new ButtonStateNormal(this);
      }
  
      public event EventHandler&lt;MouseEventArgs&gt; Click;
      public ButtonState State { get; set; }
  
      protected virtual void OnClick(MouseEventArgs e)
      {
          if (Click != null)
              Click(this, e);
      }
  }
  
  public class ButtonStateNormal : ButtonState
  {
      Button button;
      public ButtonStateNormal(Button button)
      {
          this.button = button;
      }
  
      public override void CheckForInput()
      {
          //...
          //Check for user input and change the state
          //...
          button.State = new ButtonStateDown(button);
      }
  }
  
  public class ButtonStateDown : ButtonState
  {
      Button button;
      public ButtonStateDown(Button button)
      {
          this.button = button;
      }
  
      public override void CheckForInput()
      {
          //...
          //Check for user input, fire the event of the button and change the state
          //...
          button.OnClick(new MouseEventArgs(mouse.X, mouse.Y)); //I want to do this, obviously it won't work this way
          button.State = new ButtonStateNormal(button);
      }
  }
  </code></pre>
  
  <p>How can I do that?</p>