silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight获取子控件和父控件方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

在Silverlight项目开发中,经常会把一个独立功能的控件封装成一个UserControl,然后供其他页面或者控件进行调用。前一段时间,在项目中遇到一个问题,需要在同一个页面重复调用多个相同的UserControl控件,然后在父页面中控制这些重复生成的子控件。由于很多控件是动态    在Silverlight项目开发中,经常会把一个独立功能的控件封装成一个UserControl,然后供其他页面
在Silverlight项目开发中,经常会把一个独立功能的控件封装成一个UserControl,然后供其他页面或者控件进行调用。前一段时间,在项目中遇到一个问题,需要在同一个页面重复调用多个相同的UserControl控件,然后在父页面中控制这些重复生成的子控件。由于很多控件是动态
  
在Silverlight项目开发中,经常会把一个独立功能的控件封装成一个UserControl,然后供其他页面或者控件进行调用。前一段时间,在项目中遇到一个问题,需要在同一个页面重复调用多个相同的UserControl控件,然后在父页面中控制这些重复生成的子控件。由于很多控件是动态生成数量也是动态控制,所以所有的操作都需要使用后台代码进行实现。
 
在上面的需求中需要用到Silverlight API中的VisualTreeHelper类,对于VisualTreeHelper类,有不少文章已经介绍过,该类可以对Silverlight可视化树进行遍历,该可视化树是逻辑对象树的一个子集。我们可以通过VisualTreeHelper提供的@L_616_28@GetChild(),GetParent()和GetChildrenCount(),分别获取子控件,父控件以及子控件数量
 
在实际项目中,为满足实际开发需求,对VisualTreeHelper的@L_616_28@重新进行封装是非常必要的。
 
首先要介绍的Hleper@L_616_28@是GetParentObject@L_616_28@,获取父控件@L_616_28@。该@L_616_28@将根据当前控件,遍历查找其父控件是否存在。参数1是表示当前子控件名,参数2是要查询父控件名;使用VisualTreeHelper.GetParent@L_616_28@获取当前父控件。
 
1 public T GetParentObject < T > (DependencyObject obj, String Name) where T : FrameworkElement
2 {
3 DependencyObject parent = VisualTreeHelper.GetParent(obj);
4
5 while (parent != null )
6 7 if is T && (((T)parent).Name == name | .IsNullOrEmpty(Name)))
8 9 return (T)parent;
10 }
11 12 parent VisualTreeHelper.GetParent(parent);
13 14 15 ;
16 17
 
 
另外一个Helper@L_616_28@是GetChildObject,获取子控件@L_616_28@。该@L_616_28@将根据当前控件,遍历查找其子控件是否存在。参数1是表示当前父控件名,参数2是要查询子控件名;
 
T GetChildObject DependencyObject child T grandChild for ( int i 0 ; i <= VisualTreeHelper.GetChildrenCount(obj) - ; i ++ child VisualTreeHelper.GetChild(obj,i);
(child (((T)child).Name (T)child;
else grandChild GetChildObject (child,Name);
17 (grandChild 18 grandChild;
19 20 21 22 23 24 }
 
 
最后介绍一个Helper@L_616_28@是GetChildObjects@L_616_28@,该@L_616_28@将把所有子控件作为List集合返回到客户端。其中第一个参数是父控件参数,而第二个参数是特定子控件名称,如果需要遍历全部子控件,第二个参数留空即可。
 
 
List GetChildObjects List childList new ();
|| childList.Add((T)child);
childList.AddRange(GetChildObjects "" ));
childList;
}
 
 
下面用一个例程演示使用@L_616_28@:

Silverlight获取子控件和父控件方法

 
使用@L_616_28@很简单,首先创建基础控件:
 
@H_367_301@< UserControl x:Class ="SLVisualTreeHelper.MainPage" 2 xmlns ="http://scheR_866_11845@as.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x ="http://scheR_866_11845@as.microsoft.com/winfx/2006/xaml" 4 xmlns:d ="http://scheR_866_11845@as.microsoft.com/expression/blend/2008" 5 xmlns:mc ="http://scheR_866_11845@as.openxmlformats.org/markup-compatibility/2006" 6 @H_689_316@mc:Ignorable ="d" 7 d:DesignHeight ="300" d:DesignWidth ="400" > Grid x:Name ="LayoutRoot" BACkground ="White" Grid.RowDeFinitions RowDeFinition Height ="Auto" ></ RowDeFinition ="*" </ StackPanel ="spDemoPanel" Orientation ="Vertical" Grid.Row ="0" TextBlock @H_689_316@margin ="5" 获取子控件和父控件演示 TextBlock 专注Silverlight技术交流 博客:http//jv9.cnblogs.com Button ="btDemoButton" Width ="120" Height ="40" Content ="获取所有控件" Click ="btDemoButton_Click" HorizontalAlignment ="Left" Margin /> ="btModifyChild" ="修改布局控件背景" ="btModifyChild_Click" ="btModifyChilds" ="批量修改控件字体尺寸" ="btModifyChilds_Click" StackPanel ="spResult" ="1" ="tbResult" 25 26 27 Grid 28 UserControl 29
 
 
然后在后台代码,声明实例进行调用
 
namespace SLVisualTreeHelper
partial class MainPage : UserControl
MainPage()
InitializeComponent();
private void btDemoButton_Click( object sender,RoutedEventArgs E)
Globals VTHelper Globals();
StackPanel sp VTHelper.GetChildObject ( this .LayoutRoot,0)">" spDemoPanel " );
Grid layoutGrid VTHelper.GetParentObject .spDemoPanel,0)">LayoutRoot textblock VTHelper.GetChildObjects (sp (layoutGrid (textblock.Count tbResult.Text 包含TextBlock控件数量为: + textblock.Count.ToString() \n += 获取父控件成功....\n ;
29 30 获取子控件成功....\n 31 32 33 34 btModifyChild_Click( 35 36 37 38 sp.BACkground SolidColorBrush(Colors.PurplE);
39 40 41 btModifyChilds_Click( 42 43 44 45 foreach (TextBlock tmpTextblock in textblock)
46 47 tmpTextblock.FontSize 48 49 50 51 }
 
 
其中Globals类中包含前文介绍的几个获取代码
 
在线演示:
 
 
 
最后是源代码下载,项目是VS2010+Silverlight 4, @H_618_672@

http://silverlightchina.net/html/tips/2010/0627/1342.html

大佬总结

以上是大佬教程为你收集整理的Silverlight获取子控件和父控件方法全部内容,希望文章能够帮你解决Silverlight获取子控件和父控件方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。

Silverlight获取子控件和父控件方法

点击下载