WPF 遍历DataTemplate(获取所有控件)
来源:互联网 发布:金大川项偞婧 知乎 编辑:程序博客网 时间:2024/05/05 03:10
WPF 遍历DataTemplate(获取所有控件)
分类: WPF 2011-09-02 13:29 508人阅读 评论(5)收藏 举报
情况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;
- }
2、稍微改动一下前篇里的代码:
- <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>
3、解下来就写按钮的处理函数:
我需要获取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());
- }
情况3:当没有设定DataTemplate的里的控件Name或者你压根不知道里面有哪些控件,但是你又想获取他们的值时。例如上一篇,当我动态生成CheckBox后,我想知道哪些CheckBox被选中了。
方法:
- 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;
- }
2、xaml中代码(详细请看前一篇)
- <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>
3、解下来就写按钮的处理函数:
- 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() + "被选中了!");
- }
- }
- WPF 遍历DataTemplate(获取所有控件)
- WPF 遍历DataTemplate(获取所有控件)
- WPF 遍历DataTemplate(获取所有控件)
- WP ListBox 遍历DataTemplate(获取所有控件)
- WPF 获取 ListView DataTemplate 中控件值
- WPF 点击 datatemplate 里面的控件,获取相应绑定的数据
- wpf遍历窗体中所有控件方法
- WPF DataTemplate
- 获取WPF所有控件的模板内容
- 使用VisualTreeHelper获取DataTemplate中的控件
- WPF(初识DataTemplate)
- WPF GridViewColumn Sort DataTemplate
- WPF遍历控件
- WPF遍历界面控件
- WPF中的数据模板(DataTemplate)
- WPF中的数据模板(DataTemplate)
- WPF中的数据模板(DataTemplate)
- WPF中何时使用DataTemplate
- IIS7 配置 PHP 5.x(cgi方式) | 转:橘子博客
- [搬家]2010-8-4学习轨迹
- Limit bandwidth in Mac OS X using ipfw
- [搬家]2010-8-2学习轨迹
- with 递归查询使用简记
- WPF 遍历DataTemplate(获取所有控件)
- Linux下" >/dev/null 2>&1 "相关知识说明
- LINUX TCP/IP 协议栈的关键数据结构SOCKET BUFFER(SK_BUFF )
- android 的TableLayouyt的边框的简单应用
- CSS盒子模式之四 - 绝对定位和相对定位
- spring面向切面编程(aop)
- Hibernate异常:Unable to locate appropriate constructor on class
- windows环境下 结束某个进程命令
- Java 多线程