博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF查找父元素子元素
阅读量:6678 次
发布时间:2019-06-25

本文共 1605 字,大约阅读时间需要 5 分钟。

原文:

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

                }

            }
        }

转载地址:http://jwwao.baihongyu.com/

你可能感兴趣的文章
8. Appendix: Frequently Asked Questions
查看>>
HDU:悼念512汶川大地震遇难同胞——老人是真饿了
查看>>
gulp初学
查看>>
JS设置localStorage有效期
查看>>
springmvc学习:处理方法返回值的可选类型
查看>>
Ajax常用写法
查看>>
学习Sass之安装Sass(一)
查看>>
11111 - Generalized Matrioshkas
查看>>
MongoDB基础之 用户和数据库基于角色的访问控制
查看>>
DOS运用3_SVN文件清理
查看>>
MyApp
查看>>
SqlServer字段说明查询
查看>>
键盘监听,高度获取
查看>>
android ANR
查看>>
Shell - 简明Shell入门02 - 变量(Variable)
查看>>
Shell - 简明Shell入门06 - 循环语句(Loop)
查看>>
MySQL C#连接ySQL保存当前时间,时分秒都是0,只有日期
查看>>
Aras Innovator DB备份与还原
查看>>
Java设计模式-单例模式
查看>>
git合并多个commit
查看>>