Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取MouseEvent中成员的父类_C#_Wpf_Events_Parent_Member - Fatal编程技术网

C# 获取MouseEvent中成员的父类

C# 获取MouseEvent中成员的父类,c#,wpf,events,parent,member,C#,Wpf,Events,Parent,Member,我有一个有3名成员的班级,其中只有两名是相关的;一个是多边形,另一个是坐标的int[]。我想知道多边形对应的坐标,但我真的被困在这里了 通过了解多边形坐标,我指的是我在类中沿该多边形存储的抽象立方坐标,我使用它来声明其点 坐标是X,Y和Z,我将它们存储在下面类中的int[3]中。我想做的是每次触发事件时都能捕捉到这些立方坐标 public class Tile { public int[] coords; public Polygon hex; public List&l

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

通过了解多边形坐标,我指的是我在类中沿该多边形存储的抽象立方坐标,我使用它来声明其点

坐标是X,Y和Z,我将它们存储在下面类中的int[3]中。我想做的是每次触发事件时都能捕捉到这些立方坐标

public class Tile
{
    public int[] coords;
    public Polygon hex;
    public List<object> content;
}
我在瓷砖列表上显示多边形成员,如下所示

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

是否要检查鼠标是按在多边形上还是按在其他多边形上?在多边形上,就是我按过的多边形上。您是否尝试从
发送者
获取信息?在这种情况下,它会在WPF Windows.Controls.Canvas中显示其父级的信息。
var Canvas=发送者作为Windows.Controls.Canvas
如果(canvas.Name==“polygon”){/…}
我想你可能误解了我。我已经知道了多边形与画布的坐标X,Y。我刚才说的坐标是类Tiles中的坐标,它们是立方体坐标(x,y,z),与点坐标无关。抱歉,我的问题模棱两可,现在请编辑问题以澄清问题。不管怎样,谢谢你的帮助。你现在想要什么?我更正了问题,希望现在不那么令人困惑。你想在鼠标按下事件中也获得平铺对象吗?这正是我需要的
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;
     }
}