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

Edw*_*uay 1 c# wpf grid

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

我可以使用这种语法:

CheckBox cbChange = grid.Children[4] as CheckBox;
Run Code Online (Sandbox Code Playgroud)

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

CheckBox cbChange = grid.GetXYChild(2,3) as CheckBox; //PSEUDO-CODE
Run Code Online (Sandbox Code Playgroud)

替代文字

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<string> rowNames = new List<string>
            {
                "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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

HCL*_*HCL 5

我会做一个扩展方法,取得所需的x和y值.在那里,你通过所有孩子,检查Grid.GetRow(子)和Grid.GetColumn(子) - 方法是否返回所需的值.

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

就像是:

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

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