zoukankan      html  css  js  c++  java
  • WPF 查找控件的所有子控件

            /// <summary>
            /// 查找子控件
            /// </summary>
            /// <typeparam name="T">控件类型</typeparam>
            /// <param name="parent">父控件依赖对象</param>
            /// <param name="lstT">子控件列表</param>
            public static void FindVisualChild<T>(DependencyObject parent, ref List<T> lstT) where T : DependencyObject
            {
                if (parent != null)
                {
                    T child = default(T);
                    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                    for (int i = 0; i < numVisuals; i++)
                    {
                        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                        child = v as T;
                        if (child != null)
                        {
                            lstT.Add(child);
                        }
                        FindVisualChild<T>(v, ref lstT);
                    }
                }
            }

    在DataGrid中查找选定行中的子控件使用实例:

            private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                DataGridRow currentRow = (DataGridRow)dgdRel.ItemContainerGenerator.ContainerFromIndex(dgdRel.SelectedIndex);   //获取当前行
                if (currentRow != null)
                {
                    List<Control> lstControl = new List<Control>();
                    FormDispose.FindVisualChild<Control>(currentRow, ref lstControl);  //获取当前行内所有的控件
                    if (lstControl != null)
                    {
                       //子控件的处理代码
                    }
                }
            }
  • 相关阅读:
    final、static关键字
    this关键字与super关键字区别
    JAVA常见报错
    Java抽象类和多态
    Java 类和接口的继承
    JAVA封装
    库存管理案例
    Map的遍历
    LinkedList vector集合,Set接口
    Collection,迭代器iterator,list接口
  • 原文地址:https://www.cnblogs.com/yilinyangyu/p/9012748.html
Copyright © 2011-2022 走看看