user1721413
user1721413

Reputation:

How can I get the content of a cell in a grid in C#?

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

Answers (1)

Clemens
Clemens

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

Related Questions