Question

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.

Was it helpful?

Solution

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".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top