在Silverlight项目开发中,经常会把一个独立功能的控件封装成一个UserControl,然后供其他页面或者控件进行调用。前一段时间,在项目中遇到一个问题,需要在同一个页面重复调用多个相同的UserControl控件,然后在父页面中控制这些重复生成的子控件。由于很多控件是动态生成,数量也是动态控制,所以所有的操作都需要使用后台代码进行实现。
在上面的需求中需要用到Silverlight API中的VisualTreeHelper类,对于VisualTreeHelper类,有不少文章已经介绍过,该类可以对Silverlight可视化树进行遍历,该可视化树是逻辑对象树的一个子集。我们可以通过VisualTreeHelper提供的方法GetChild(),GetParent()和GetChildrenCount(),分别获取子控件,父控件以及子控件数量。
在实际项目中,为满足实际开发需求,对VisualTreeHelper的方法重新进行封装是非常必要的。
首先要介绍的Hleper方法是GetParentObject方法,获取父控件方法。该方法将根据当前控件,遍历查找其父控件是否存在。参数1是表示当前子控件名,参数2是要查询父控件名;使用VisualTreeHelper.GetParent方法获取当前父控件。
2 {
3 DependencyObject parent = VisualTreeHelper.GetParent(obj);
4
5 while (parent != null )
6 {
7 if (parent is T && (((T)parent).Name == name | string .IsNullOrEmpty(name)))
8 {
9 return (T)parent;
10 }
11
12 parent = VisualTreeHelper.GetParent(parent);
13 }
14
15 return null ;
16 }
17
2 {
3 DependencyObject child = null ;
4 T grandChild = null ;
5
6 for ( int i = 0 ; i <= VisualTreeHelper.GetChildrenCount(obj) - 1 ; i ++ )
7 {
8 child = VisualTreeHelper.GetChild(obj, i);
9
10 if (child is T && (((T)child).Name == name | string .IsNullOrEmpty(name)))
11 {
12 return (T)child;
13 }
14 else
15 {
16 grandChild = GetChildobject < T > (child, name);
17 if (grandChild != null )
18 return grandChild;
19 }
20 }
21
22 return null ;
23
24 }
最后介绍一个Helper方法是GetChildobjects方法,该方法将把所有子控件作为List集合返回到客户端。其中第一个参数是父控件参数,而第二个参数是特定子控件名称,如果需要遍历全部子控件,第二个参数留空即可。
2 {
3 DependencyObject child = null ;
4 List < T > childList = new List < T > ();
5
6 for ( int i = 0 ; i <= VisualTreeHelper.GetChildrenCount(obj) - 1 ; i ++ )
7 {
8 child = VisualTreeHelper.GetChild(obj, i);
9
10 if (child is T && (((T)child).Name == name || string .IsNullOrEmpty(name)))
11 {
12 childList.Add((T)child);
13 }
14
15 childList.AddRange(GetChildobjects < T > (child, "" ));
16 }
17
18 return childList;
19
20 }
使用方法很简单,首先创建基础控件:
2 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 mc:Ignorable ="d"
7 d:DesignHeight ="300" d:DesignWidth ="400" >
8
9 < Grid x:Name ="LayoutRoot" Background ="White" >
10 < Grid.RowDeFinitions >
11 < RowDeFinition Height ="Auto" ></ RowDeFinition >
12 < RowDeFinition Height ="*" ></ RowDeFinition >
13 </ Grid.RowDeFinitions >
14 < StackPanel x:Name ="spDemoPanel" Orientation ="Vertical" Grid.Row ="0" >
15 < TextBlock Margin ="5" > 获取子控件和父控件演示 </ TextBlock >
16 < TextBlock Margin ="5" > 专注Silverlight技术交流 </ TextBlock >
17 < TextBlock Margin ="5" > 博客:http//jv9.cnblogs.com </ TextBlock >
18 < Button x:Name ="btDemoButton" Width ="120" Height ="40" Content ="获取所有控件" Click ="btDemoButton_Click" HorizontalAlignment ="Left" Margin ="5" />
19 < Button x:Name ="btModifyChild" Width ="120" Height ="40" Content ="修改布局控件背景" Click ="btModifyChild_Click" HorizontalAlignment ="Left" Margin ="5" />
20 < Button x:Name ="btModifyChilds" Width ="120" Height ="40" Content ="批量修改控件字体尺寸" Click ="btModifyChilds_Click" HorizontalAlignment ="Left" Margin ="5" />
21 </ StackPanel >
22
23 < StackPanel x:Name ="spResult" Grid.Row ="1" >
24 < TextBlock x:Name ="tbResult" />
25 </ StackPanel >
26
27 </ Grid >
28 </ UserControl >
29
2 {
3 public partial class MainPage : UserControl
4 {
5
6 public MainPage()
7 {
8 InitializeComponent();
9 }
10
11 private void btDemoButton_Click( object sender, RoutedEventArgs e)
12 {
13 Globals VTHelper = new Globals();
14 StackPanel sp = VTHelper.GetChildobject < StackPanel > ( this .LayoutRoot, " spDemoPanel " );
15 Grid layoutGrid = VTHelper.GetParentObject < Grid > ( this .spDemoPanel, " LayoutRoot " );
16 List < TextBlock > textblock = VTHelper.GetChildobjects < TextBlock > ( this .LayoutRoot, "" );
17
18
19 if (sp != null )
20 {
21 if (layoutGrid != null )
22 {
23
24 if (textblock.Count > 0 )
25 {
26 tbResult.Text = " 包含TextBlock控件数量为: " + textblock.Count.ToString() + " \n " ;
27 }
28 tbResult.Text += " 获取父控件成功....\n " ;
29 }
30 tbResult.Text += " 获取子控件成功....\n " ;
31 }
32 }
33
34 private void btModifyChild_Click( object sender, RoutedEventArgs e)
35 {
36 Globals VTHelper = new Globals();
37 StackPanel sp = VTHelper.GetChildobject < StackPanel > ( this .LayoutRoot, " spDemoPanel " );
38 sp.Background = new SolidColorBrush(Colors.Purple);
39 }
40
41 private void btModifyChilds_Click( object sender, RoutedEventArgs e)
42 {
43 Globals VTHelper = new Globals();
44 List < TextBlock > textblock = VTHelper.GetChildobjects < TextBlock > ( this .LayoutRoot, "" );
45 foreach (TextBlock tmpTextblock in textblock)
46 {
47 tmpTextblock.FontSize += 6 ;
48 }
49 }
50 }
51 }
在线演示:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。