如何在WPF和Silverlight中取得DataTemplate中的命名元素
2010-01-26 09:44
591 查看
WPF
1.假如ListBox使用如下的DataTemplate
代码
ListBoxItem myListBoxItem =
(ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
TextBlock myTextBlock = FindFirstVisualChild<TextBlock>(_myListBoxItem , "textBlock");
MessageBox.Show("The text of the TextBlock of the selected list item: "
+ myTextBlock.Text);
private childItem FindVisualChild<childItem>(DependencyObject obj,String childName)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem && child.GetValue(NameProperty).ToString()==childName)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child,childName);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
1.假如ListBox使用如下的DataTemplate
代码
ListBoxItem myListBoxItem =
(ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
TextBlock myTextBlock = FindFirstVisualChild<TextBlock>(_myListBoxItem , "textBlock");
MessageBox.Show("The text of the TextBlock of the selected list item: "
+ myTextBlock.Text);
private childItem FindVisualChild<childItem>(DependencyObject obj,String childName)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem && child.GetValue(NameProperty).ToString()==childName)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child,childName);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
相关文章推荐
- silverlight:ListBox中如何取得DateTemplate/ItemsPanelTemplate中的命名控件?
- WPF silverlight获取子控件(获取DataTemplate里的子控件)
- silverlight wpf DataTemplate Command binding
- WPF/Silverlight HierarchicalDataTemplate 模版的使用
- WPF中如何使用C#创建DataTemplate数据模版
- WPF/Silverlight HierarchicalDataTemplate 模版的使用
- 如何:查找由 DataTemplate 生成的元素
- wpf如何获取control template里的元素
- WPF : 如何使用DataTemplateSelector
- [Silverlight学习笔记]如何获取ItemsControl的DataTemplate中定义的控件?
- Silverlight 获取DataTemplate 中可视化元素
- WP7 ListBox中如何取得DateTemplate/ItemsPanelTemplate中的命名控件?
- [Silverlight学习笔记]如何获取ItemsControl的DataTemplate中定义的控件?
- wpf如何获取control template里的元素
- wpf如何获取control template里的元素
- WPF 遍历DataTemplate(获取所有控件)
- WPF 点击 datatemplate 里面的控件,获取相应绑定的数据
- Silverlight模拟WPF的TemplateSelector,以实现动态显示不同column
- Silverlight:获取ContentTemplate中的命名控件
- Silverlight中TreeView,TreeViewItem,HierarchalDataTemplate的详细用法