如何在C#中获取网格中单元格的内容?

4

我需要获取C#中Grid中单元格的内容。是否有类似以下方法来达成目的?

UIElement element = MyGrid.Children.getElementAt(x, y)

x和y是屏幕坐标还是行列索引? - jam40jeff
1个回答

11

你可以使用 Linq:

// using System.Linq;

var element = grid.Children.Cast<UIElement>().
    FirstOrDefault(e => Grid.GetColumn(e) == x && Grid.GetRow(e) == y);

或者如果在指定的单元格中有多个元素:

var elements = grid.Children.Cast<UIElement>().
    Where(e => Grid.GetColumn(e) == x && Grid.GetRow(e) == y);

其中 elements 是一个 IEnumerable<UIElement>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接