4.如何获取动态生成的SL控件的NAME值(二)

试验目的:使用AS动态获取页面所有的控件(这些控件包括动态生成的与直接在XAML里面添加的)
1.as与IS的使用(IS与AS的使用专门用C#补上去)
2.SL页面控件的加载循序,先加载XAML页面定义的控件,再就是加载.CS文件里动态添加的控件


什么是UIElement?
UIElement 是 Silverlight 中具有可视外观并可以处理基本输入的大多数对象的基类。
1.所有继承这个System.Windows.UIElement的,都具有可视化的功能

.xaml

 1 <UserControl x:Class="sl19.MainPage"
 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     <Canvas x:Name="parentCanvas"    Background="Maroon" >
 9         <Button x:Name="mmm" Margin="10" Background="Blue" Canvas.ZIndex="1" Canvas.Top="150" Canvas.Left="150" Height="30" Width="149" Content="我是从页面文件添加的"></Button>
10        
11     </Canvas>
12 </UserControl>

.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using System.Text;
13 using System.Windows.Markup;
14 namespace sl19
15 {
16     public partial class MainPage : UserControl
17     {
18         public MainPage()
19         {
20             InitializeComponent();
21             StringBuilder xaml = new StringBuilder();
22             xaml.Append("<Button ");
23             xaml.Append("xmlns=\"http://schemas.microsoft.com/client/2007\" ");
24             xaml.Append("Canvas.Left=\"50\" Canvas.Top=\"30\"  FontSize=\"20\" ");
25             xaml.Append(" FontWeight=\"Bold\" Content=\"动态创建XAML对象\" />");
26             //创建textBlock
27             Button button1 = (Button)XamlReader.Load(xaml.ToString());
28             //  button1.Name = "button1";
29             button1.Click += new RoutedEventHandler(button1_Click);
30             parentCanvas.Children.Add(button1);
31             //line的xaml文本
32             xaml = new StringBuilder();
33             xaml.Append("<Line Stroke=\"Red\" ");
34             xaml.Append("xmlns=\"http://schemas.microsoft.com/client/2007\" ");
35             xaml.Append(" X1=\"30\" Y1=\"30\" ");
36             xaml.Append(" X2=\"200\" Y2=\"200\"  StrokeThickness=\"3\" />");
37             //创建LINE对象
38             Line line = (Line)XamlReader.Load(xaml.ToString());
39             line.Name = "line1";
40             parentCanvas.Children.Add(line);
41 
42         }
43         //Click后要执行的方法,已绑定  
44         void button1_Click(object sender, RoutedEventArgs e)
45         {
46             Button btn = sender as Button;     
47          
48             
49             Button btn1;
50             Line line1;
51             foreach (UIElement tmp_ui in this.parentCanvas.Children)
52             {
53                 btn1 = tmp_ui as Button;//如果得到的对象不是Button,返回是NULL
54                 if (btn1 != null)
55                 {
56                     MessageBox.Show(" 我是一个Button!!!我的显示值是"+ btn1.Content.ToString());           
57                 }
58                 else if (btn1==null)
59                 {
60                     line1 = tmp_ui as Line;
61                     if (line1!=null)
62                     {
63                         MessageBox.Show(" 我是一个LINE!!!我的NAME值是" + line1.Name.ToString());
64                     }
65                }
66             }           
67         }
68     }
69 }
70 

最后结论:
第一种获取动态生成的控件是:
  btn.Content += "-" + tmp_ui.GetValue(NameProperty).ToString();// tmp_ui.GetValue(ContentProperty).ToString();
第二种是AS IS转换控件btn1 = tmp_ui as Button;  转换为自己想要的控件
                

原文地址:https://www.cnblogs.com/muer/p/1741150.html