如何使用x / y坐标而不是索引访问Grid中的子项? [英] How to access children in Grid with x/y coordinates instead of index?
问题描述
我可以使用这种语法:
$ b我有一个Grid对象,并希望获得特定的复选框。 $ b
CheckBox cbChange = grid.Children [4]作为CheckBox;
但是我怎样才能通过x / y坐标来访问这个孩子,例如:
CheckBox cbChange = grid.GetXYChild(2,3)作为CheckBox; // PSEUDO-CODE
using System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
namespace TestGrid92292
{
public partial class Window1:Window
{
public Window1()
{
InitializeComponent() ;
列表< string> rowNames =新列表< string>
{
营销,
销售,
开发
};
Grid grid = new Grid();
grid.Margin =新厚度(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(rowNames中的var rowName)
{
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 ++;
}
//检查特定框
// CheckBox cbChange = grid.GetXYChild(2,3)作为CheckBox;
CheckBox cbChange = grid.Children [4]作为CheckBox;
cbChange.IsChecked = true;
MainContent.Children.Add(grid);
我会制作一个扩展方法,它可以获得所需的x值和y值。在这里你通过所有的孩子,并检查Grid.GetRow(child)和Grid.GetColumn(child) - 方法是否返回所需的值。
因此我会返回一个 IEnumerable< FrameworkElement>
,因为你可以有多个,一个或者没有元素在这个位置(不是在你的例子中,而是一般的这种方法)。
类似于:
public static class GridExtensions {
public static IEnumerable< FrameworkElement> GetXYChild(这个网格实例,int x,int y){
if(null == instance){
throw new ArgumentNullException(instance);
}
List< FrameworkElement> list = new List< FrameworkElement>();
foreach(instance.Children中的FrameworkElement fe){
if(Grid.GetRow(fe)== y&& Grid.GetColumn(fe)== x){
list。加入(FE);
}
}
返回列表;
}
}
我没有测试过它,如果发表评论不起作用。如果您只想返回一个实例,则可以根据需要更改代码。
I have a Grid object and want to get a specific checkbox out of it.
I can use this syntax:
CheckBox cbChange = grid.Children[4] as CheckBox;
But how can I access this child via x/y coordinate instead, e.g.:
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);
}
}
}
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.
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).
Something like:
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中的子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!