5

To add a click event to a Button in C# code behind, I can do this

Button btn = new Button;
btn.Click += btn_Click;

What if I have an Ellipse, which does not contain a Click?

Ellipse e = new Ellipse;
e.??? += e_Click;
KMC
  • 19,548
  • 58
  • 164
  • 253

2 Answers2

8

One way to do that is make button an ellipse and attatch .Click Event handler.

<Button>
    <Button.Template>
        <ControlTemplate>
            <Ellipse .../>
        </ControlTemplate>
    </Button.Template>
</Button>
Sanjeevakumar Hiremath
  • 10,985
  • 3
  • 41
  • 46
6

Maybe the MouseUp event will serve your purpose. Try

Ellipse ellipse = new Ellipse();
ellipse.MouseUp += ellipse_MouseUp;

private void ellipse_MouseUp(object sender, MouseButtonEventArgs e)
{
   ...
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • 5
    MouseUp != Click, just saying. – H.B. Apr 06 '11 at 03:44
  • @H.B. agreed but since there is no Click, you'll have to pick one of MouseUp, MouseDown, MouseLeftButtonDown, MouseLeftButtonUp, MouseRightButtonDown, MouseRightButtonUp. – Bala R Apr 06 '11 at 03:51
  • I posed a question about this actually, you can create a click from those events or use behaviours, it's all a bit messy though, the cleanest thing would be to wrap it in a button but who knows if this would be an option here. (My Q: http://stackoverflow.com/questions/4830164/what-is-the-best-way-to-simulate-a-click-with-mouseup-mousedown-events-or-other ) – H.B. Apr 06 '11 at 04:34