Search code examples
c#wpfxamlblend

How to add more than one event for button click within a single event handler in Expression Blend?


I have a button and an ellipse, and I need to change the fill colour of the same ellipse on three successive clicks, e.g first click - fill colour is red, 2nd click - yellow and 3rd click - green. How can I do this using a single event handler for the button click?


Solution

  • There are loads of ways you can do it, depending on how big your application is. If its significant, read about state pattern and change the state each time you do a click.

    Or if its small you can do something like this .. (please treat this as pseudo Code)

    private string _lastState = "DefaultState" 
    
    public void MyClickHandler()
    {
        ChangeState();
    }
    
    private void ChangeState()
    {
    Switch (_lastState)
    case "Default": _lastState = "Red";
                _myControl.Backgroung = Red; 
    case "Red": _lastState = "Yellow";
                _myControl.Backgroung = Yellow;
    case "Yellow": _lastState = "Green";
                _myControl.Backgroung = Green;
    
    
    .. and so on..
    
    }
    
    where _myControl is your state .. may be just a string variable..
    
    }