c# - 在 MouseEvent 中获取成员的父类

标签 c# wpf events parent member

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

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

坐标是 X、Y 和 Z,我将它们存储在以下类的 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;
     }
}

关于c# - 在 MouseEvent 中获取成员的父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32013779/

相关文章:

C# 和 Entity Framework - 为具有不确定家谱的自引用实体优化数据库查询

c# - 使用 SC/服务控制安装服务

javascript - 开箱即用的 Light Switch HTML 应用程序在 IE9 中出现空错误?

c# - 嵌套用户控件数据上下文

c# - 如何获取我的全文目录中使用的停用词列表?

c# - 如何在 wpf 中比较 if((sender as Grid).Background== new SolidColorBrush(Colors.Green))

更改焦点和滚动时,带有 IsVirtualizing ="true"的 WPF TreeView 会跳来跳去

javascript - 此示例中的 JavaScript 事件处理有什么问题? (使用click()和hover() jQuery方法)

javascript - 向上遍历,然后向下遍历 DOM

javascript - 在 keyup 函数中未获取键码