Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wpf

row

grid

Once Controls have been added to a WPF Grid, is there a way to programmatically access them by row and/or column index? Something along the lines of:

 var myControl = (object)MyGrid.GetChild(int row, int column); 

... where GetChild is the method I wish I had!

like image 516
Mathias Avatar asked Oct 02 '09 20:10

Mathias


People also ask

How do you add rows and columns to a WPF grid programmatically?

RowDefinitions. Add(gridRow3); Once rows and columns are added to Grid, you can add any contents to Grid cells by using SetRow and SetColumn methods. SetRow and SetColumn methods take first parameter as the control name and second parameter as row number and column number respectively.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.

What is grid splitter in WPF?

A GridSplitter is a divider that divides a Grid into two sections. A GridSplitter allows us to resize rows or columns in a Grid by dragging the GridSplitter Bar. An example of a GridSplitter is the Windows Explorer.


1 Answers

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); 
like image 77
itowlson Avatar answered Sep 26 '22 01:09

itowlson