zoukankan      html  css  js  c++  java
  • WPF查找父元素子元素

            /// <summary>
            /// WPF中查找元素的父元素
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="i_dp"></param>
            /// <returns></returns>
            public static T FindParent<T>(DependencyObject i_dp) where T : DependencyObject
            {
                DependencyObject dobj = (DependencyObject)VisualTreeHelper.GetParent(i_dp);
                if (dobj != null)
                {
                    if (dobj is T)
                    {
                        return (T)dobj;
                    }
                    else
                    {
                        dobj = FindParent<T>(dobj);
                        if (dobj != null && dobj is T)
                        {
                            return (T)dobj;
                        }
                    }
                }
                return null;
            }

            /// <summary>
            ///WPF查找元素的子元素
            /// </summary>
            /// <typeparam name="childItem"></typeparam>
            /// <param name="obj"></param>
            /// <returns></returns>
            private childItem FindVisualChild<childItem>(DependencyObject obj)
        where childItem : DependencyObject
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is childItem)
                        return (childItem)child;
                    else
                    {
                        childItem childOfChild = FindVisualChild<childItem>(child);
                        if (childOfChild != null)
                            return childOfChild;
                    }
                }
                return null;
            }

           //测试listView获取焦点后,将符合条件的的项选中

           private void listView_GotFocus(object sender, RoutedEventArgs e)
            {
                DependencyObject dobj = e.OriginalSource as DependencyObject;
                if (null != dobj)
                {
                    System.Windows.Controls.ListViewItem gvc = FindParent<System.Windows.Controls.ListViewItem>(dobj);
                    if (gvc != null)
                    {
                        System.Windows.Controls.ListView listView = FindParent<System.Windows.Controls.ListView>(gvc);
                        if (listView != null)
                        {
                            int index = 0;
                            foreach (var lv in listView.ItemsSource)
                            {
                                if ((FTPFile)gvc.DataContext == lv)
                                {
                                    listView.SelectedIndex = index;
                                    break;
                                }
                                index++;
                            }
                        }

                    }
                }
            }

  • 相关阅读:
    计算机代数系统Computer Algebra Software: Mathematica, Maxima, Pari/GP
    计算机代数系统对比:Computer Algebra Software: Mathematica, Maxima, Pari/GP
    搜索大质数的算法PHP版本【算法导论实践】
    使用maxima解决初等数论中的问题:
    练习使用文法剖析工具:
    反查字符的unicode码~
    Maxima Overview:
    Elementary number theory using Maxima
    Word文档结构图内容“越界”问题:
    Maxima, Maple, and Mathematica: the Summary~
  • 原文地址:https://www.cnblogs.com/_ymw/p/2915870.html
Copyright © 2011-2022 走看看