将控件添加到 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];
网格对象的 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;