简体   繁体   English

Windows Phone使用网格

[英]Windows Phone using Grid

I have a grid with 7 rows and 7 columns. 我有一个7行7列的网格。 I want to put in each cell a control dynamically. 我想动态地在每个单元格中放入一个控件。

To add the controls I use this code 要添加控件,我使用此代码

Rectangle newRectangle = new Rectangle();

newRectangle.Tap += new EventHandler<GestureEventArgs>(Rectangle_KeyDown);
newRectangle.Fill = HighlightColor;

Grid.SetColumn(newRectangle, i);
Grid.SetRow(newRectangle, ii);

grid1.Children.Add(newRectangle);

How can I get one of those controls from position x,y ? 如何从位置x,y获得其中一个控件?

I thought something like 我觉得有点像

Grid.GetColumn( ?? );
Grid.GetRow( ?? );

But I don't know how to continue. 但我不知道如何继续。

I realy hope someone can help me. 我真的希望有人可以帮助我。

There is no built in function for that so you should do the searching manually. 没有内置功能,所以你应该手动进行搜索。 But you can easily write such search function eg with Linq: 但是你可以轻松地用Linq编写这样的搜索功能:

var rectangleAtXy = grid.Children.OfType<Rectangle>()
    .SingleOrDefault(c => Grid.GetColumn(c) == x && Grid.GetRow(c) == y);

There is no function for this. 这没有任何功能。 You have to read the attached properties Row amd Column of the grid's children to determine in which cell they are in. 您必须阅读网格子项的附加属性RowColumn ,以确定它们所在的单元格。

I am not sure what you are trying to accomplish there but I might suggest a different, cleaner approach that might work for you. 我不确定你想要在那里完成什么,但我可能会建议一种可能对你有用的不同的,更清洁的方法。

It involves using a ListBox with a UniformGrid as the ItemsPanelTemplate. 它涉及使用带有UniformGrid的ListBox作为ItemsPanelTemplate。 You would then create a Collection and set it as the ItemsSource for this List. 然后,您将创建一个Collection并将其设置为此List的ItemsSource。 You can now populate your list with your Controls using a simple transformation from a bi dimensional perspective (col, row) to a single dimension list (your list). 现在,您可以使用从二维透视图(col,row)到单个维度列表(列表)的简单转换,使用控件填充列表。 Setting and retrieving the controls is now as simple as that transformation. 现在,设置和检索控件就像转换一样简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM