Reputation: 13
How can I locate an ui-element by Its location (row and column)?
Upvotes: 1
Views: 2176
Reputation: 582
Great answer. My implementation (Generating array of Buttons in code then want indexed array thereof):
Button[,] Buttons = new Button[8, endTimePlus1 - startTime];
var buttons = controlGrid.Children.Cast<Button>();
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < endTimePlus1 - startTime; j++)
{
Buttons[i,j] = buttons.Where(x => Grid.GetRow(x) == j && Grid.GetColumn(x) == i).FirstOrDefault();
}
}
Upvotes: 0
Reputation: 6472
UIElement FindByCell1(Grid g, int row, int col)
{
var childs = g.Children.Cast<UIElement>()
return childs.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == col).FirstOrDefault();
}
If there may be some elements in the same cell:
IEnumerable<UIElement> FindByCell(Grid g, int row, int col)
{
var childs = g.Children.Cast<UIElement>()
return childs.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == col);
}
In your case, This way - working directly with the elements in the UI - its very very not recommended, and its the extreme opposite of MVVM.
Upvotes: 1