zoukankan      html  css  js  c++  java
  • Silverlight中获取控件中子控件

    如题:,直接来看代码:

    /// <summary>
            /// 查找并返回第一个 相同 name的子元素
            /// </summary>
            /// <typeparam name="T">需要查找 的子控件 类型</typeparam>
            /// <param name="obj">需要查找其下面子控件的  控件 类型</param>
            /// <param name="childName">子控件 的name</param>
            /// <returns></returns>
            public static T FindFirstVisualChild<T>(DependencyObject obj, string childName) where T : DependencyObject
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is T && child.GetValue(System.Windows.FrameworkElement.NameProperty).ToString() == childName)
                    {
                        return (T)child;
                    }
                    else
                    {
                        T childOfChild = FindFirstVisualChild<T>(child, childName);
                        if (childOfChild != null)
                        {
                            return childOfChild;
                        }
                    }
                }
                return null;
            }

    大概就是这样子啦,上面那个方法的 参数等等东西都说明的很清楚了。这次的需求是在ListBox中点击后,需要确定点击的是哪个,需要把id传到第二个页面来确定第二个页面展示什么东西。而我的ListBox中都是Tile(windows phone),因此就通过下面点击事件来获取我需要的id

    private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                Tile b = (sender) as Tile;
                //查找到控件c4f下面隐藏的textblock中的值,也就是id的值
                TextBlock c = iSCAU.Tools.DomeHandle.FindFirstVisualChild<TextBlock>(b, "mark");
                NavigationService.Navigate(new Uri("/SyncFood/FoodMenu.xaml?id="+c.Text, UriKind.Relative));
                
            }
  • 相关阅读:
    Selenium的使用
    Redis防护建议
    爬虫文件存储-3:Redis
    爬虫文件存储-2:MongoDB
    爬虫文件存储-1:mysql
    爬虫文件存储:txt文档,json文件,csv文件
    Python MongoDB 教程
    使用Robo 3T 软件管理MongoDB数据库如何执行命令行shell
    使用Scrapy爬取图书网站信息
    解决Scrapy抓取中文网页保存为json文件时中文不显示而是显示unicode的问题
  • 原文地址:https://www.cnblogs.com/xmfdsh/p/3997049.html
Copyright © 2011-2022 走看看