Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parent class of member in MouseEvent

I have a class with 3 members, only two of them relevant; one is a Polygon and the other a int[] of coordinates. I want to be aware of that Polygon corresponding coordinates but I am really stuck here.

By aware of the polygon coordinates I mean the abstract cubic coordinates I store in the class along that polygon and which I use to declare its Points.

The coordinates are X, Y and Z and I store them in an int[3] in the following class. And what I want to do is to be able to catch those cubic coordinates everytime the event is triggered.:

public class Tile
{
    public int[] coords;
    public Polygon hex;
    public List<object> content;
}

The List filler method:

        foreach (int[] i in ValidCoordinates)
        {
            int[] coords = i;

            double apotema = Math.Sqrt(Math.Pow(20, 2) - Math.Pow(20 / 2, 2));

            double auxX = x + (coords[0] * (20 * 3 / 2));
            double auxY = y + (coords[0] * apotema + (coords[1] * apotema * 2));

            Polygon poly = Hex.HexagonalPolygon(20, auxX, auxY);

            poly.Fill = Brushes.Blue;

            Hexagon.Tile tile = new Hexagon.Casilla();

            tile.coords = coords;
            tile.hex = poly;

            ListTiles.Add(tile);
        }

And I show the Polygon member on the List of Tiles like this...

foreach (Hexagon.Tile t in ListTiles)
{
    PolyCanvas.Children.Add(t.hex);
}

And then find the Polygon with a MouseEvent and change it's properties:

private void PolyCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.OriginalSource is Polygon)
    {
        Polygon poly = e.OriginalSource as Polygon;

        poly.Fill = Brushes.Red;
    }
}

A bit more of code... XMAL:

<Grid Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="400"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Canvas Name="PolyCanvas" MouseDown="PolyCanvas_MouseDown" Height="700" Width="1200">
    </Canvas>
    <TextBox Name="txt" Grid.Row="1"></TextBox>
</Grid>

I have thoroughly searched for an answer but I'm clueless.

like image 694
Commit Avatar asked Nov 10 '22 07:11

Commit


1 Answers

Try this

foreach (int[] i in ValidCoordinates)
{
     int[] coords = i;

     double apotema = Math.Sqrt(Math.Pow(20, 2) - Math.Pow(20 / 2, 2));

     double auxX = x + (coords[0] * (20 * 3 / 2));
     double auxY = y + (coords[0] * apotema + (coords[1] * apotema * 2));

     Polygon poly = Hex.HexagonalPolygon(20, auxX, auxY);

     poly.Fill = Brushes.Blue;

     Hexagon.Tile tile = new Hexagon.Casilla();

     tile.coords = coords;
     tile.hex = poly;
     ListTiles.Add(tile);
}
...
//put values into tile and then set as tag
PolyCanvas.Tag = tile;
...
private void PolyCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{    
     var canvas = sender as System.Windows.Controls.Canvas;
     if (canvas.Name == "PolyCanvas")
     {
          if(canvas.Tag != null)
             var tiles = (Tile)canvas.Tag;
     }
}
like image 78
NASSER Avatar answered Nov 15 '22 07:11

NASSER