How to access children in Grid with x/y coordinates instead of index?(如何使用 x/y 坐标而不是索引访问 Grid 中的子项?)
问题描述
我有一个 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 中的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 x/y 坐标而不是索引访问 Grid 中的子项?


基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01