silverlight 获取 ControlTemplate 中的控件
StringBuilder strStyle = new StringBuilder(); strStyle.Append("<Style xmlns='http://schemas.microsoft.com/client/2007' xmlns:sdk='http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Key=\"STYLE_DATAGRIDCELL\" TargetType=\"sdk:DataGridCell\"> "); strStyle.Append(" <Setter Property=\"Foreground\" Value=\"Blue\" /> "); strStyle.Append(" <Setter Property=\"Cursor\" Value=\"Hand\"></Setter> "); strStyle.Append(" <Setter Property=\"Template\" > "); strStyle.Append(" <Setter.Value> "); strStyle.Append(" <ControlTemplate> "); strStyle.Append(" <TextBlock x:Name=\"UrlTextName\" VerticalAlignment=\"Center\" Tag=\"{Binding}\" Text=\"{Binding " + name + "}\" /> "); strStyle.Append(" </ControlTemplate> "); strStyle.Append(" </Setter.Value> "); strStyle.Append(" </Setter> "); strStyle.Append("</Style> "); Style style = (Style)XamlReader.Load(strStyle.ToString());
06. <TextBlock x:Name="txtName" Text="Good"/>
07. </WrapPanel>
08. </Border>
09. </DataTemplate>
10.</Page.Resources>
11.
12.<ItemsControl x:Name="itemsControl" Background="#B28BB2F1" ItemTemplate="{StaticResource data}">
13. <ItemsControl.ItemsPanel>
14. <ItemsPanelTemplate>
15. <WrapPanel Orientation="Horizontal"/>
16. </ItemsPanelTemplate>
17. </ItemsControl.ItemsPanel>
18.</ItemsControl>
3、解下来就写按钮的处理函数:
我需要获取DataTemplate里名为"txtName"的TextBlock控件并显示他的Text内容。
[csharp] view plaincopy
01.private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
02.{
03. TextBlock txt = FindFirstVisualChild<TextBox>(itemsControl, "txtName");
04. if (txt != null)//判断是否找到
05. MessageBox.Show(txt.Text.ToString());
06.}
情况3:当没有设定DataTemplate的里的控件Name或者你压根不知道里面有哪些控件,但是你又想获取他们的值时。例如上一篇,当我动态生成CheckBox后,我想知道哪些CheckBox被选中了。
方法:
1、也需要一个获取DataTemplate控件的函数,但是返回的是一个集合。
[csharp] view plaincopy
01.public List<T> GetChildObjects<T>(DependencyObject obj, string name) where T : FrameworkElement
02.{
03. DependencyObject child = null;
04. List<T> childList = new List<T>();
05. for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
06. {
07. child = VisualTreeHelper.GetChild(obj, i);
08. if (child is T && (((T)child).Name == name || string.IsNullOrEmpty(name)))
09. {
10. childList.Add((T)child);
11. }
12. childList.AddRange(GetChildObjects<T>(child, ""));//指定集合的元素添加到List队尾
13. }
14. return childList;
15. }
2、xaml中代码(详细请看前一篇)
[html] view plaincopy
01.<ItemsControl x:Name="itemsControl" Background="#B28BB2F1">
02. <ItemsControl.ItemsPanel>
03. <ItemsPanelTemplate>
04. <WrapPanel Orientation="Horizontal"/>
05. </ItemsPanelTemplate>
06. </ItemsControl.ItemsPanel>
07. <ItemsControl.ItemTemplate>
08. <DataTemplate>
09. <Border Padding="3">
10. <WrapPanel>
11. <CheckBox Content="{Binding txt}"/>
12. </WrapPanel>
13. </Border>
14. </DataTemplate>
15. </ItemsControl.ItemTemplate>
16.</ItemsControl>
3、解下来就写按钮的处理函数:
[csharp] view plaincopy
01.private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
02.{
03. DataVisualTreeHelper VTHelper = new DataVisualTreeHelper();
04. List<CheckBox> collection = VTHelper.GetChildObjects<CheckBox>(itemsControl, "")//第2个参数为空,表示查找所有指定类型的控件(返回
05.
06.一个CheckBox集合)
07. foreach (CheckBox item in collection //遍历这个集合
08. {
09. if (item.IsChecked == true)
10. MessageBox.Show(item.Content.ToString() + "被选中了!");
11. }
12.}