How to programmatically access Control in WPF Grid by row and column index?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 18:44:21

There isn't a built-in method for this, but you can easily do it by looking in the Children collection:

myGrid.Children
      .Cast<UIElement>()
      .First(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == column);
Carlo

This answer will help you

int rowIndex = Grid.GetRow(myButton);

RowDefinition rowDef = myGrid.RowDefinitions[rowIndex];

The Children property of the grid object will give you a collection of all the children of the Grid (from the Panel class).

As far as getting the coordinates in the grid, look at the static methods in the Grid class (GetRow() & GetColumn()).

Hope that sets you off in the right direction.

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;
    }
}

you could just give your grid column/row a name

<Grid x:Name="MainGridBackground" Grid.Column="0"/>

and access it programmatically by calling it and using "."

MainGridBackground.Background = canvasUCInstance.rectanglePreview.Fill;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!