情况1:在设定DataTemplate的Name,并且他是在前台表示时,获取DataTemplate里的指定控件。
方法:
方法:
1、这里需要有一个从DataTemplate里获取控件的函数public T FindFirstVisualChild<T>(DependencyObject obj, string childName) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null && child is T && child.GetValue(NameProperty).ToString() == childName) { return (T)child; } else { T childOfChild = FindFirstVisualChild<T>(child, childName); if (childOfChild != null) { return childOfChild; } } } return null; }
<ItemsControl x:Name="itemsControl" Background="#B28BB2F1"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border Padding="3"> <WrapPanel> <TextBox x:Name="txtID"/> <TextBlock x:Name="txtName" Text="Good"/> </WrapPanel> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
或者
<Page.Resource> <DataTemplate x:Key="data"> <Border Padding="3"> <WrapPanel> <TextBox x:Name="txtID"/> <TextBlock x:Name="txtName" Text="Good"/> </WrapPanel> </Border> </DataTemplate> </Page.Resources> <ItemsControl x:Name="itemsControl" Background="#B28BB2F1" ItemTemplate="{StaticResource data}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl>
我需要获取DataTemplate里名为"txtName"的TextBlock控件并显示他的Text内容。
private void Button_Click(object sender, System.Windows.RoutedEventArgs e) { TextBlock txt = FindFirstVisualChild<TextBox>(itemsControl, "txtName"); if (txt != null)//判断是否找到 MessageBox.Show(txt.Text.ToString()); }
方法:
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, ""));//指定集合的元素添加到List队尾 } return childList; }
<ItemsControl x:Name="itemsControl" Background="#B28BB2F1"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border Padding="3"> <WrapPanel> <CheckBox Content="{Binding txt}"/> </WrapPanel> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
private void Button_Click(object sender, System.Windows.RoutedEventArgs e) { DataVisualTreeHelper VTHelper = new DataVisualTreeHelper(); List<CheckBox> collection = VTHelper.GetChildObjects<CheckBox>(itemsControl, "")//第2个参数为空,表示查找所有指定类型的控件(返回 一个CheckBox集合) foreach (CheckBox item in collection //遍历这个集合 { if (item.IsChecked == true) MessageBox.Show(item.Content.ToString() + "被选中了!"); } }