Reputation:
I need to get the content of cell in a Grid
in C#. Is there a way to do something like this?
UIElement element = MyGrid.Children.getElementAt(x, y)
Upvotes: 4
Views: 8240
Reputation: 128061
You could use Linq:
// using System.Linq;
var element = grid.Children.Cast<UIElement>().
FirstOrDefault(e => Grid.GetColumn(e) == x && Grid.GetRow(e) == y);
or if there is more than one element in the specified cell:
var elements = grid.Children.Cast<UIElement>().
Where(e => Grid.GetColumn(e) == x && Grid.GetRow(e) == y);
where elements is an IEnumerable<UIElement>
.
Upvotes: 11