在 MouseEvent 中获取成员的父 class

Get parent class of member in MouseEvent

我有一个 class 有 3 个成员,其中只有两个相关;一个是多边形,另一个是坐标的 int[]。我想知道那个多边形对应的坐标,但我真的被困在这里了。

通过了解多边形坐标,我的意思是我存储在该多边形 class 中的抽象立方坐标,我用它来声明它的点。

坐标为X、Y、Z,我将它们存储在下面class中的一个int[3]中。而我想要做的是能够在每次触发事件时捕获那些立方坐标。:

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

列表填充方法:

        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);
        }

然后我在 Tiles 列表中显示 Polygon 成员是这样的...

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

然后找到带有 MouseEvent 的多边形并更改其属性:

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

        poly.Fill = Brushes.Red;
    }
}

更多代码... 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>

我已经彻底搜索了答案,但我一无所知。

试试这个

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;
     }
}