Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用x/y坐标而不是索引访问网格中的子级?_C#_Wpf_Grid - Fatal编程技术网

C# 如何使用x/y坐标而不是索引访问网格中的子级?

C# 如何使用x/y坐标而不是索引访问网格中的子级?,c#,wpf,grid,C#,Wpf,Grid,我有一个网格对象,希望从中获得一个特定的复选框 我可以使用以下语法: CheckBox cbChange = grid.Children[4] as CheckBox; 但是我如何通过x/y坐标来访问这个孩子,例如: CheckBox cbChange = grid.GetXYChild(2,3) as CheckBox; //PSEUDO-CODE 使用System.Collections.Generic; 使用System.Windows; 使用System.Windows.Contr

我有一个网格对象,希望从中获得一个特定的复选框

我可以使用以下语法:

CheckBox cbChange = grid.Children[4] as CheckBox;
但是我如何通过x/y坐标来访问这个孩子,例如:

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

使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
命名空间TestGrid92292
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
列表行名称=新列表
{
“营销”,
“销售”,
“发展”
};
网格=新网格();
网格边缘=新厚度(5);
ColumnDefinition col1=新ColumnDefinition();
col1.Width=新的GridLength(100,GridUnitType.Pixel);
grid.ColumnDefinitions.Add(col1);
ColumnDefinition col2=新ColumnDefinition();
col2.Width=新的GridLength(1,GridUnitType.Star);
grid.ColumnDefinitions.Add(col2);
ColumnDefinition col3=新ColumnDefinition();
col3.Width=新的GridLength(1,GridUnitType.Star);
grid.ColumnDefinitions.Add(col3);
int rowCount=0;
foreach(rowNames中的var rowName)
{
RowDefinition行=新的RowDefinition();
grid.RowDefinitions.Add(行);
TextBlock tb=新的TextBlock();
tb.Text=rowName;
tb.SetValue(Grid.ColumnProperty,0);
tb.SetValue(Grid.RowProperty,rowCount);
grid.Children.Add(tb);
复选框cb=新复选框();
cb.SetValue(Grid.ColumnProperty,1);
cb.SetValue(Grid.RowProperty,rowCount);
cb.HorizontalAlignment=HorizontalAlignment.Left;
grid.Children.Add(cb);
复选框cb2=新复选框();
cb2.SetValue(Grid.ColumnProperty,2);
cb2.SetValue(Grid.RowProperty,rowCount);
cb2.HorizontalAlignment=HorizontalAlignment.Left;
grid.Children.Add(cb2);
行计数++;
}
//选中一个特定的框
//复选框cbChange=grid.GetXYChild(2,3)作为复选框;
复选框cbChange=grid.Children[4]作为复选框;
cbChange.IsChecked=true;
MainContent.Children.Add(网格);
}
}
}
请参见:

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

片段:

其中
x
是网格中一行可以包含的项目数
x
可以是
grid.COLUMNDEFINITIONS.Count
。然而,只有在网格是统一的情况下,这才有效

(我对WPF没有太多的了解,所以我不知道是否有更直观的方法)

请参见:

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

片段:

其中
x
是网格中一行可以包含的项目数
x
可以是
grid.COLUMNDEFINITIONS.Count
。然而,只有在网格是统一的情况下,这才有效


(我对WPF没有太多的研究,所以我不知道是否有更直观的方法)

我将创建一个扩展方法,该方法采用所需的x和y值。在这里,您将遍历所有子对象,并检查Grid.GetRow(子对象)和Grid.GetColumn(子对象)方法是否返回所需的值

因此,我将返回一个
IEnumerable
,因为在这个位置可以有多个、一个或没有元素(不是在您的示例中,而是对于这种方法)

比如:

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;
    }
}
公共静态类GridExtensions{
公共静态IEnumerable GetXYChild(此网格实例,int x,int y){
if(null==实例){
抛出新的ArgumentNullException(“实例”);
}
列表=新列表();
foreach(instance.Children中的FrameworkElement fe){
if(Grid.GetRow(fe)==y&&Grid.GetColumn(fe)==x){
列表。添加(fe);
}
}
退货清单;
}
}

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

我将创建一个扩展方法,该方法采用所需的x和y值。在这里,您将遍历所有子对象,并检查Grid.GetRow(子对象)和Grid.GetColumn(子对象)方法是否返回所需的值

因此,我将返回一个
IEnumerable
,因为在这个位置可以有多个、一个或没有元素(不是在您的示例中,而是对于这种方法)

比如:

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;
    }
}
公共静态类GridExtensions{
公共静态IEnumerable GetXYChild(此网格实例,int x,int y){
if(null==实例){
抛出新的ArgumentNullException(“实例”);
}
列表=新列表();
foreach(instance.Children中的FrameworkElement fe){
if(Grid.GetRow(fe)==y&&Grid.GetColumn(fe)==x){
列表。添加(fe);
}
}
退货清单;
}
}
我没有测试它,如果它不起作用,请发表评论。如果只想返回一个实例,可以相应地修改代码