views:

138

answers:

1

Hello,

I am studding Silverlight. I have an application where I create Polygons in UserControl_Loaded method. During creation stage I add MouseLeftButtonUp event handler like this:

polygon.MouseLeftButtonUp += MouseButtonEventHandler_MouseLeftButtonUp;

All polygons have the same handler.

My goal is to use a custom object when I click on a polygon.

For instance, I have two polygons; both of them have int MyCustomInt32 property. The property is set during creation stage. For the first polygon it is set to 10, for the second one to 20. When the event fires I would like to retrieve and set MyCustomInt32 value. Of course, the value should be different, it depends on which polygon I click.

Is it possible to do in Silverlight?

Thank you.

+1  A: 

You can cast the sender parameter to your custom class type:-

private void MouseButtonEventHandler_MouseLeftButtonUp(object sender,  MouseButtonEventArgs e)
{
    var polygon = (MyCustomPolygon)sender;
    int x = polygon.MyCustomInt32;
}

Edit:

In reponse to your comment, the subject of actually creating an implementation of a custom control is too wide. There are however plenty of articles out in web land to review. A couple of examples are:-

There are plenty more found with the simple web search "Custom Control Silverlight".

AnthonyWJones
Thank you for the answer. Could you please provide an code snippet how to implement MyCustomPolygon in you answer? Is it possible to make a new class and inherit it from Polygon class?
Antipod
You can't inherit from Polygon, its sealed. You can create a custom control which contains a Polygon.
AnthonyWJones
Thank you. I will try.
Antipod