Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF, how can I determine what column/row in a grid a control is in?

Tags:

wpf

grid

I am building a grid dynamically and putting buttons in one of the columns. When I click a button, I want to know what row of my grid it's in. How can I find this?

like image 273
ScottG Avatar asked Dec 12 '08 15:12

ScottG


People also ask

What is grid row in WPF?

A Grid Panel provides a flexible area which consists of rows and columns. In a Grid, child elements can be arranged in tabular form. Elements can be added to any specific row and column by using Grid.Row and Grid.Column properties. By default, a Grid panel is created with one row and one column.

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.

How do I create a dynamic grid in WPF?

The Grid class in WPF represents a Grid control. The following code snippet creates a Grid control, sets its width, horizontal alignment, vertical alignment, show grid lines, and background color. Grid DynamicGrid = new Grid();

What is Grid view in WPF?

The GridView view mode displays a list of data items by binding data fields to columns and by displaying a column header to identify the field. The default GridView style implements buttons as column headers.


1 Answers

In the Click event handler for the button you say:

int row;
Button btn = sender as Button;
if (btn != null)
{
    row = Grid.GetRow(btn); // And you have the row number...
}
else
{
    // A nasty error occurred...
}
like image 167
Boyan Avatar answered Sep 22 '22 00:09

Boyan