2014-03-26 30 views
1

如何根據其位置(行和列)來定位UI元素?如何按列和行號選擇網格中的子項

+2

Web?視窗? WPF? –

+1

您可以發佈您的calss定義代碼嗎?它將幫助我們幫助你。 –

+0

還指出你使用了什麼結構?數組,自定義? –

回答

1
UIElement FindByCell1(Grid g, int row, int col) 
{ 
    var childs = g.Children.Cast<UIElement>() 
    return childs.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == col).FirstOrDefault(); 
} 

如果有可能在同一小區的一些元素:

IEnumerable<UIElement> FindByCell(Grid g, int row, int col) 
{ 
    var childs = g.Children.Cast<UIElement>() 
    return childs.Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == col); 
} 

在你的情況下,這種方式 - 它非常非常不推薦,它的 - 直接與UI元素工作與MVVM極端相反。