Reputation: 525
I have a grid with 7 rows and 7 columns. I want to put in each cell a control dynamically.
To add the controls I use this code
Rectangle newRectangle = new Rectangle();
newRectangle.Tap += new EventHandler<GestureEventArgs>(Rectangle_KeyDown);
newRectangle.Fill = HighlightColor;
Grid.SetColumn(newRectangle, i);
Grid.SetRow(newRectangle, ii);
grid1.Children.Add(newRectangle);
How can I get one of those controls from position x,y
?
I thought something like
Grid.GetColumn( ?? );
Grid.GetRow( ?? );
But I don't know how to continue.
I realy hope someone can help me.
Upvotes: 4
Views: 558
Reputation: 8039
I am not sure what you are trying to accomplish there but I might suggest a different, cleaner approach that might work for you.
It involves using a ListBox with a UniformGrid as the ItemsPanelTemplate. You would then create a Collection and set it as the ItemsSource for this List. You can now populate your list with your Controls using a simple transformation from a bi dimensional perspective (col, row) to a single dimension list (your list). Setting and retrieving the controls is now as simple as that transformation.
Upvotes: 1
Reputation: 139798
There is no built in function for that so you should do the searching manually. But you can easily write such search function e.g with Linq:
var rectangleAtXy = grid.Children.OfType<Rectangle>()
.SingleOrDefault(c => Grid.GetColumn(c) == x && Grid.GetRow(c) == y);
Upvotes: 4
Reputation: 20764
There is no function for this. You have to read the attached properties Row
amd Column
of the grid's children to determine in which cell they are in.
Upvotes: 1