如何在WPF和Silverlight中取得DataTemplate中的命名元素
WPF
1.假如ListBox使用如下的DataTemplate
代码
<DataTemplate x:Key="myDataTemplate">
<TextBlock Name="textBlock" FontSize="14" Foreground="Blue">
<TextBlock.Text>
<Binding XPath="Title"/>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
<TextBlock Name="textBlock" FontSize="14" Foreground="Blue">
<TextBlock.Text>
<Binding XPath="Title"/>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
2.在cs文件中写如下的代码可以找到某个ListBoxItem对应的TextBlock
代码
ListBoxItem myListBoxItem =
(ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);
MessageBox.Show("The text of the TextBlock of the selected list item: "
+ myTextBlock.Text);
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
(ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);
MessageBox.Show("The text of the TextBlock of the selected list item: "
+ myTextBlock.Text);
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
SilverLight
1. Xmal 和WPF中相同
2. 由于SL中的DataTemplate没有FindName方法,因此c#代码有所区别
代码
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;
}
(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;
}