如何获得网格中的按钮? [英] How to get the button in the Grid?

查看:64
本文介绍了如何获得网格中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网格中有2行2列,每行有一个按钮和一个标签.

如何获得具有行和列值的按钮?

像这样:

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:

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



有人帮我吗?非常感谢!

PS:最重要的是如何同时定位行和列?
我可以找到"Grid.ColumnDefinitions []或Grid.RowDefinitions []".



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[]".

推荐答案

使用此函数可以获取所有网格中的按钮:

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

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



之后,反复使用Grid.GetColumn(element)Grid.GetRow(element)抛出它们并观察它们的列号和行号.

希望对您有所帮助.



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

Hope it helps.


Get WPF DataGrid row and cell[^] should eventually help you get to the button.

Use VisualTreeHelper within the cell to get the button.


这篇关于如何获得网格中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆