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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14185102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:55:22  来源:igfitidea点击:

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

c#wpfxamlgrid

提问by Clemens

I need to get the content of cell in a Gridin C#. Is there a way to do something like this?

我需要Grid在 C# 中获取单元格的内容。有没有办法做这样的事情?

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

回答by Clemens

You could use Linq:

你可以使用 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>.

其中元素是一个IEnumerable<UIElement>