如何使用x / y坐标而不是索引访问Grid中的子项?

我有一个Grid对象,想要从中获取一个特定的复选框。

我可以使用这种语法:

CheckBox cbChange = grid.Children[4] as CheckBox; 

但是我如何通过x / y坐标来访问这个孩子,例如:

 CheckBox cbChange = grid.GetXYChild(2,3) as CheckBox; //PSEUDO-CODE 

替代文字

 using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace TestGrid92292 { public partial class Window1 : Window { public Window1() { InitializeComponent(); List rowNames = new List { "Marketing", "Sales", "Development" }; Grid grid = new Grid(); grid.Margin = new Thickness(5); ColumnDefinition col1 = new ColumnDefinition(); col1.Width = new GridLength(100, GridUnitType.Pixel); grid.ColumnDefinitions.Add(col1); ColumnDefinition col2 = new ColumnDefinition(); col2.Width = new GridLength(1, GridUnitType.Star); grid.ColumnDefinitions.Add(col2); ColumnDefinition col3 = new ColumnDefinition(); col3.Width = new GridLength(1, GridUnitType.Star); grid.ColumnDefinitions.Add(col3); int rowCount = 0; foreach (var rowName in rowNames) { RowDefinition row = new RowDefinition(); grid.RowDefinitions.Add(row); TextBlock tb = new TextBlock(); tb.Text = rowName; tb.SetValue(Grid.ColumnProperty, 0); tb.SetValue(Grid.RowProperty, rowCount); grid.Children.Add(tb); CheckBox cb = new CheckBox(); cb.SetValue(Grid.ColumnProperty, 1); cb.SetValue(Grid.RowProperty, rowCount); cb.HorizontalAlignment = HorizontalAlignment.Left; grid.Children.Add(cb); CheckBox cb2 = new CheckBox(); cb2.SetValue(Grid.ColumnProperty, 2); cb2.SetValue(Grid.RowProperty, rowCount); cb2.HorizontalAlignment = HorizontalAlignment.Left; grid.Children.Add(cb2); rowCount++; } //check a specific box //CheckBox cbChange = grid.GetXYChild(2,3) as CheckBox; CheckBox cbChange = grid.Children[4] as CheckBox; cbChange.IsChecked = true; MainContent.Children.Add(grid); } } } 

我会做一个扩展方法,取得所需的x和y值。 在那里,您将浏览所有子项并检查Grid.GetRow(子)和Grid.GetColumn(子) – 方法是否返回所需的值。

因此,我将返回一个IEnumerable因为在这个位置可以有多个,一个或没有元素(不是在你的例子中,而是一般的这种方法)。

就像是:

 public static class GridExtensions { public static IEnumerable GetXYChild(this Grid instance, int x, int y) { if (null == instance) { throw new ArgumentNullException("instance"); } List list = new List(); foreach (FrameworkElement fe in instance.Children) { if (Grid.GetRow(fe) == y && Grid.GetColumn(fe) == x) { list.Add(fe); } } return list; } } 

我没有对它进行测试,如果不起作用则发表评论。 如果您只想返回一个实例,则可以相应地更改代码。

看到:

http://www.trillian.com.au/2009/01/how-to-get-element-from-grid-using-xy.html

片段:

 public static Collection GetElements(this Grid grid, int row, int column) where TElement : UIElement { var elements = from UIElement element in grid.Children where element is TElement && Grid.GetRow(element) == row && Grid.GetColumn(element) == column select element as TElement; return new Collection(elements.ToList()); } 

或我的部分解决方案如下:

如果你的坐标是i = rowj = column那么:

 CheckBox cbChange = grid.Children[i * x + j - 1] as CheckBox; 

其中x是一行可以包含在网格中的项目数。 x可以是grid.ColumnDifinitions.Count 。 但是,只有当你的网格是统一的时,这才有效。

(我没有和WPF一起工作太多,所以我不知道是否有更直观的方式)