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

概述

在Silverlight项目开发中,经常会把一个独立功能的控件封装成一个UserControl,然后供其他页面或者控件进行调用。前一段时间,在项目中遇到一个问题,需要在同一个页面重复调用多个相同的UserControl控件,然后在父页面中控制这些重复生成的子控件。由于很多控件是动态生成,数量也是动态控制,所以所有的操作都需要使用后台代码进行实现。   在上面的需求中需要用到Silverlight

在Silverlight项目开发中,经常会把一个独立功能的控件封装成一个UserControl,然后供其他页面或者控件进行调用。前一段时间,在项目中遇到一个问题,需要在同一个页面重复调用多个相同的UserControl控件,然后在父页面中控制这些重复生成的子控件。由于很多控件是动态生成数量也是动态控制,所以所有的操作都需要使用后台代码进行实现。

 

在上面的需求中需要用到Silverlight API中的VisualTreeHelper类,对于VisualTreeHelper类,有不少文章已经介绍过,该类可以对Silverlight可视化树进行遍历,该可视化树是逻辑对象树的一个子集。我们可以通过VisualTreeHelper提供的方法GetChild(),GetParent()和GetChildrenCount(),分别获取子控件,父控件以及子控件数量

 

在实际项目中,为满足实际开发需求,对VisualTreeHelper的方法重新进行封装是非常必要的。

 

首先要介绍的Hleper方法是GetParentObject方法获取父控件方法。该方法将根据当前控件,遍历查找其父控件是否存在。参数1是表示当前子控件名,参数2是要查询父控件名;使用VisualTreeHelper.GetParent方法获取当前父控件。

public T GetParentObject<T>(DependencyObject obj,String Name) where T : FrameworkElement
         {
             DependencyObject parent = VisualTreeHelper.GetParent(obj);
 
             while (parent != null)
             {
                 if (parent is T && (((T)parent).Name == name | String.IsNullOrEmpty(Name)))
                 {
                     return (T)parent;
                 }
 
                 parent = VisualTreeHelper.GetParent(parent);
             }
 
             return null;
         }


 

另外一个Helper方法是GetChildObject,获取子控件方法。该方法将根据当前控件,遍历查找其子控件是否存在。参数1是表示当前父控件名,参数2是要查询子控件名;

public T GetChildObject<T>(DependencyObject obj,String Name) where T : FrameworkElement
         {
             DependencyObject child = null;
             T grandChild = null;
 
             for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
             {
                 child = VisualTreeHelper.GetChild(obj,i);
 
                 if (child is T && (((T)child).Name == name | String.IsNullOrEmpty(Name)))
                 {
                     return (T)child;
                 }
                 else
                 {
                     grandChild = GetChildObject<T>(child,Name);
                     if (grandChild != null)
                         return grandChild;
                 }
             }
 
             return null;
 
         }


 

最后介绍一个Helper方法是GetChildObjects方法,该方法将把所有子控件作为List集合返回到客户端。其中第一个参数是父控件参数,而第二个参数是特定子控件名称,如果需要遍历全部子控件,第二个参数留空即可。

public List<T> GetChildObjects<T>(DependencyObject obj,String Name) where T : FrameworkElement
         {
             DependencyObject child = null;
             List<T> childList = new List<T>();
 
             for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
             {
                 child = VisualTreeHelper.GetChild(obj,i);
 
                 if (child is T && (((T)child).Name == name || String.IsNullOrEmpty(Name)))
                 {
                     childList.Add((T)child);
                 }
 
                 childList.AddRange(GetChildObjects<T>(child,""));
             }
 
             return childList;
 
         }


 

下面用一个例程演示使用方法

 

 

使用方法很简单,首先创建基础控件:

<UserControl x:Class="SLVisualTreeHelper.MainPage"
     xmlns="http://scheR_44_11845@as.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://scheR_44_11845@as.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://scheR_44_11845@as.microsoft.com/expression/blend/2008"
     xmlns:mc="http://scheR_44_11845@as.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d"
     d:DesignHeight="300" d:DesignWidth="400">
 
     <Grid x:Name="LayoutRoot" BACkground="White">
         <Grid.RowDeFinitions>
             <RowDeFinition Height="Auto"></RowDeFinition>
             <RowDeFinition Height="*"></RowDeFinition>
         </Grid.RowDeFinitions>
         <StackPanel x:Name="spDemoPanel" Orientation="Vertical" Grid.Row="0">
             <TextBlock Margin="5">获取子控件和父控件演示</TextBlock>
             <TextBlock Margin="5">专注Silverlight技术交流</TextBlock>
             <TextBlock Margin="5">博客:http//jv9.cnblogs.com</TextBlock>
             <Button x:Name="btDemoButton" Width="120" Height="40" Content="获取所有控件" Click="btDemoButton_Click" HorizontalAlignment="Left" Margin="5"/>
             <Button x:Name="btModifyChild" Width="120" Height="40" Content="修改布局控件背景" Click="btModifyChild_Click" HorizontalAlignment="Left" Margin="5"/>
             <Button x:Name="btModifyChilds" Width="120" Height="40" Content="批量修改控件字体尺寸" Click="btModifyChilds_Click" HorizontalAlignment="Left" Margin="5"/>
         </StackPanel>
         
         <StackPanel x:Name="spResult" Grid.Row="1">
             <TextBlock x:Name="tbResult"/>
         </StackPanel>
 
     </Grid>
 </UserControl>


 

然后在后台代码,声明实例进行调用

namespace SLVisualTreeHelper
 {
     public partial class MainPage : UserControl
     {
         
         public MainPage()
         {
             InitializeComponent();
         }
 
         private void btDemoButton_Click(object sender,RoutedEventArgs E)
         {
             Globals VTHelper = new Globals();
             StackPanel sp = VTHelper.GetChildObject<StackPanel>(this.LayoutRoot,"spDemoPanel");
             Grid layoutGrid = VTHelper.GetParentObject<Grid>(this.spDemoPanel,"LayoutRoot");
             List<TextBlock> textblock = VTHelper.GetChildObjects<TextBlock>(this.LayoutRoot,"");
          
            
             if (sp != null)
             {
                 if (layoutGrid != null)
                 {
                     
                     if (textblock.Count > 0)
                     {
                         tbResult.Text = "包含TextBlock控件数量为: " + textblock.Count.ToString() + "\n";
                     }
                     tbResult.Text += "获取父控件成功....\n" ;
                 }
                 tbResult.Text += "获取子控件成功....\n";
             }
         }
 
         private void btModifyChild_Click(object sender,"spDemoPanel");
             sp.BACkground = new SolidColorBrush(Colors.PurplE); 
         }
 
         private void btModifyChilds_Click(object sender,RoutedEventArgs E)
         {
             Globals VTHelper = new Globals();
             List<TextBlock> textblock = VTHelper.GetChildObjects<TextBlock>(this.LayoutRoot,"");
             foreach (TextBlock tmpTextblock in textblock)
             {
                 tmpTextblock.FontSize += 6;
             }
         }
     }
 }

大佬总结

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

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

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