问题描述
我有一个 Grid 对象,想从中取出一个特定的复选框.
I have a Grid object and want to get a specific checkbox out of it.
我可以使用这个语法:
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<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);
}
}
}
推荐答案
我会创建一个扩展方法来获取所需的 x 和 y 值.在那里,您遍历所有子项并检查 Grid.GetRow(child) 和 Grid.GetColumn(child) 方法是否返回所需的值.
I would make an extension method that takes the desired x-and y-values. There in you go through all children and check if the Grid.GetRow(child) and Grid.GetColumn(child)-methods return the desired values.
因此我会返回一个 IEnumerable<FrameworkElement>
因为你可以在这个位置有多个、一个或没有元素(不是在你的例子中,而是一般的这种方法).
As a result I would return an IEnumerable<FrameworkElement>
because you can have more than one, one or no elements at this position (not in your example, but for such a method in general).
类似:
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;
}
}
我没有测试过,如果不起作用,请发表评论.如果您只想返回一个实例,您可以相应地更改代码.
I have not tested it, make a comment if it does not work. If you only want to return one instance, you can change the code acordingly.
这篇关于如何使用 x/y 坐标而不是索引访问 Grid 中的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!