Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There are 2 rows and 2 columns in a grid,and each one have one button and one lable.

How can I get the button with the value of the row and the column?

Like this:

C#
GetButton(string column_value,string row_value)
{
        //How to do it?
        return button;
}


Anyone Helps me? Thanks very much!

PS:The most important is how to location the row and the column at the same time?
I can just find "Grid.ColumnDefinitions[] or Grid.RowDefinitions[]".
Posted
Updated 31-Jan-12 15:14pm
v3

Use this function to get all the buttons in your grid:

C#
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }


After that iterate throw them and observer their column and row number using Grid.GetColumn(element) and Grid.GetRow(element).

Hope it helps.
 
Share this answer
 
Get WPF DataGrid row and cell[^] should eventually help you get to the button.

Use VisualTreeHelper within the cell to get the button.
 
Share this answer
 
v2
Comments
Elan.Cao 31-Jan-12 5:23am    
I'sorry,I can't open this link.I'm learning WPF new,can you tell more?
Abhinav S 31-Jan-12 6:36am    
You can get the cell by using return grid.GetCell(rowContainer, column);

Then use VisualTreeHelper on the cell to get the button inside it.
Elan.Cao 31-Jan-12 22:06pm    
Sorry,I really can't find a method like "grid.GetCell(rowContainer, column)" you said.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900