Get parent member class in MouseEvent

I have a class with 3 members, only two of them are relevant; one is a polygon and the other is an int [] coordinate. I want to know about the corresponding Polygon coordinates, but I'm really stuck here.

Knowing the coordinates of the polygon, I mean the abstract cubic coordinates that I store in the class along this polygon and which I use to declare my points.

The X, Y, and Z coordinates and I store them in int [3] in the following class. And what I want to do is to catch these cubic coordinates every time the event fires .:

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

List overlay 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 a member of Polygon in the list of slabs like this ...

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

And then find Polygon with MouseEvent and change its 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 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 carefully searched for the answer, but I do not know.

+5
source share
1 answer

try it

 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; } } 
+1
source

All Articles