一旦将控件添加到WPF网格中,是否有一种可以通过行和/或列索引以编程方式访问它们的方法?例如:
var myControl = (object)MyGrid.GetChild(int row, int column);
...其中GetChild
是我希望拥有的方法!
没有内置的方法可以做到这一点,但是您可以通过查看Children集合来轻松实现:
myGrid.Children
.Cast<UIElement>()
.First(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == column);
这个答案将会帮助到您。
int rowIndex = Grid.GetRow(myButton);
RowDefinition rowDef = myGrid.RowDefinitions[rowIndex];
Grid对象的Children属性将为您提供Grid(来自Panel类)的所有子项的集合。
至于在网格中获取坐标,请查看Grid类中的静态方法(GetRow()和GetColumn())。
希望这能让您朝着正确的方向前进。
System::Windows::Controls::Grid ^ myGrid = nullptr; System::Windows::Controls::UserControl ^ pUserControl = nullptr;
在编程方面,将以上内容翻译成中文。myGrid = m_DlgOwnedObjAdmin->GrdProperties;
if (myGrid->Children->Count > 0)
{
pUserControl = (System::Windows::Controls::UserControl^)myGrid->Children->default[0];
if (pUserControl != nullptr)
{
if (bValue == true)
pUserControl->Visibility = System::Windows::Visibility::Visible;
else
pUserControl->Visibility = System::Windows::Visibility::Collapsed;
}
}
你可以给你的网格列/行起一个名字
<Grid x:Name="MainGridBackground" Grid.Column="0"/>
并通过调用它并使用“.”来编程访问它
MainGridBackground.Background = canvasUCInstance.rectanglePreview.Fill;