如何在WPF的网格中获取指定行和列的对象? [英] How to get the object of specified row and colum in WPF's Grid?

查看:1349
本文介绍了如何在WPF的网格中获取指定行和列的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如确保在第一行和第一行中确保对象.

Such as get the object sure in Row one and Colum one.

推荐答案

WPF网格的行和列属性实际上是附加的依赖项属性.因此,必须包括某些静态方法,以便这些属性在XAML中可用.您只需要遍历Grid的子级以确保遍历Grid中的对象,然后使用相同的方法来获取对象的列和行.

以下代码是如何执行此操作的简单示例.请记住,它是在每个项目的列和行跨度为1的前提下运行的.为了安全起见,您还需要抓住对象的列和行的跨度,然后执行进行一些数学运算,以确保对象与所需位置重叠.

The row and column properties for a WPF Grid are really attached dependency properties. So, certain static methods must be included inorder for those properties to be usable in XAML. You only need to loop through the children of the Grid to make sure you''re going through the objects in the grid, and then you use those same methods to get the column and row of the object.

The following code is a simple example of how to do this. Please keep in mind that it runs under the assumption that each item has a column and row span of 1. In order to be on the safe side, you will also to have grab the spans of the objects'' columns and rows, and do a little math to be sure an object overlaps the position you want.

foreach (UIElement item in someGrid.Children)
{
   int column = Grid.GetColumn(item);
   int row = Grid.GetRow(item);

   if (column == expectedColumn && row == expectedRow)
   {
      // Do something.
   }
}



请参阅此论坛:
social.msdn.microsoft.com/Forums/en/wpf/线程/74332b78-6bfd-4ac9-af85-dfd9bec87a29

refer to this forum:
social.msdn.microsoft.com/Forums/en/wpf/thread/74332b78-6bfd-4ac9-af85-dfd9bec87a29


这篇关于如何在WPF的网格中获取指定行和列的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆